2017-04-24 31 views
-2

嗨我尝试使用下面的代码来使用从foodpanda网站的下拉列表,并选择城市名称,但它不适合我。下拉不与硒这个网站

public static void main(String args[]) throws InterruptedException{ 
     System.setProperty("webdriver.chrome.driver","C:/chromedriver.exe"); 
     WebDriver driver = new ChromeDriver(); 
     driver.get("https://www.foodpanda.in/restaurants/city/pune?gclid=CIbFi5iEvdMCFdeFaAodujsK5w"); 
     Thread.sleep(5000); 


     WebElement drp =driver.findElement(By.id("cityId")); 
     Select drp2 = new Select(drp); 
     drp2.selectByVisibleText("Bangalore"); 

它给出了在线程的错误 - 异常“主要” org.openqa.selenium.ElementNotVisibleException:元素不可见:元素当前不可见,不得操纵

+0

为什么不工作? – alayor

+0

它说 - ElementNotVisibleException:元素不可见:元素当前不可见,可能不会被操纵 –

+0

为什么不尝试'ExplicitWait'? – Paras

回答

1

<select>元件CSS display: none;。它只是一个占位符。 我认为该网站使用一些花哨的<span> s来获得下拉的真棒外观。你能检查一下吗?

0

所以我试图弄清楚下拉是如何工作的&这里是我发现的。 select只是一个占位符,它在网页上没有任何作用。当单击input框(请参见下文)时,将使用javascript调用填充下拉列表。

enter image description here

一旦点击input箱内,一些DOM元素添加含有<p>标签与城市名称,点击它时,选择您要选择的目标城市。

基于我的观察,当输入元素被点击时,类名为tt-suggestionsspan是可见的。此span内有div,其中包含所有p元素。您应该瞄准点击相应的p元素,其中包含您想要的城市名称的文本。

这里有一个工作代码:

WebDriverWait wait = new WebDriverWait(driver, 30); 
driver.findElement(
    By.xpath("//input[@class='form-control twitter-typeahead tt-input']") 
).click(); // this will click inside the input element 
WebElement drp = (WebElement) wait.until(ExpectedConditions.presenceOfElementLocated(
       By.xpath("//span[@class='tt-suggestions']"))); // this is the span element which gets populated 
System.out.println(drp.getAttribute("innerHTML")); // just in case you want to see the above span's HTML code 
drp.findElement(By.xpath("//div/p[text()[contains(.,'Bangalore')]]")).click(); // This will select Bangalore from the Dropdown 
+0

您好我在线程“main”中添加wait-异常之后出现此错误org.openqa.selenium.TimeoutException:在等待By.id定位的元素的可见性30秒后超时:cityId –

+0

@Jayesh Doolani,No,the元素是永久不可见的,所以selenium click不可能在这个地方(因为selectByVisibleText在内部做点击操作)。 – RAJ

+0

@SladeWilsonXXX我修改了我的答案。尝试这个 –