Selenium WebDriver等待元素并点击。

3
InternetExplorerDriver  driver = new InternetExplorerDriver();

driver.get("http://www.wiki-doctor.com");
WebElement element = (new WebDriverWait(driver, 10))
               .until(ExpectedConditions.elementToBeClickable(By.id("btnSearch")));
element.click();

它等待元素btnSearch显示并点击它,但似乎什么都没有发生,你有任何想法为什么会这样吗?

谢谢


1
你使用的是哪个浏览器?当我在Chrome中运行你的代码时,会弹出一个警告框,上面写着“请插入地址”。 - Reez0
我正在使用Internet Explorer - dtjmsy
String service = "C:\ToolsQA\IEDriverServer_x64_3.7.0\IEDriverServer.exe"; System.setProperty("webdriver.ie.driver", service); InternetExplorerDriver driver = new InternetExplorerDriver(); - dtjmsy
3个回答

3

这将添加美国作为语言环境,并等待医生的图片显示。

driver.get("http://www.wiki-doctor.com");
//Enter united states into field
driver.findElement(By.id("field-findDoctor-town")).sendKeys("United States");
WebElement element = (new WebDriverWait(driver, 10))
               .until(ExpectedConditions.elementToBeClickable(By.id("btnSearch")));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);

WebDriverWait wait = new WebDriverWait(driver,10);
//Wait for picture of doctor
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#dvparcticianlist > div.row > div > div.listing-row > div.doc-photo-container")));
       System.out.println("Search Successful");
   }

嗨,Reezo,关于参数[0]的问题,那是什么?这个对象从哪里来的?谢谢。 - dtjmsy
请查看Selenium ExecuteScript页面上的定义。参数将通过“arguments”魔术变量提供给JavaScript,就像通过“Function.apply”调用函数一样。而executeScript的返回值是:返回值:布尔值、长整型、字符串、列表或WebElement中的一个。或者为空。这意味着返回对象是一个列表,您可以通过“arguments[0]”魔术变量与之交互。 - Reez0

1
页面加载完毕后,我们首先需要等待意图中的WebElement(即第一个搜索框)可点击。然后,我们将在两个搜索框输入一些文本,并在搜索按钮上调用 "click()" 方法,如下所示:
System.setProperty("webdriver.ie.driver", "C:\\Utility\\BrowserDrivers\\IEDriverServer.exe");
WebDriver  driver = new InternetExplorerDriver();
driver.get("http://www.wiki-doctor.com");
WebElement locality = (new WebDriverWait(driver, 5))
       .until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='field-findDoctor-town']")));
locality.sendKeys("Pune");
driver.findElement(By.xpath("//input[@id='speciality']")).sendKeys("Doctor");
driver.findElement(By.xpath("//button[@id='btnSearch']")).click();

我尝试了你的代码,但仍然没有任何反应,甚至搜索框也没有填充,就像这些ID从未被找到一样。 - dtjmsy
我认为这与我的Internet Explorer版本和下载驱动程序有关,因为在Chrome中代码运行得非常好。 - dtjmsy
请查看我的更新答案并告诉我状态。 - undetected Selenium

-1
WebDriverWait wait =new WebDriverWait(driver, 90);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("xpath")));     
driver.findElement(By.xpath("xpath")).click();

6
请努力解释您的答案。 - Martin Spamer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接