【问题标题】:Selenium webdriver wait element and clickSelenium webdriver 等待元素并单击
【发布时间】:2017-11-24 11:28:15
【问题描述】:
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 显示并单击它,但是,它似乎什么也没做,你知道为什么会这样吗?

谢谢

【问题讨论】:

  • 你用的是什么浏览器?当我使用 chrome 运行您的代码时,我收到一条警告说“请插入地址”。
  • 我正在使用 Internet Explorer
  • 字符串服务 = "C:\\ToolsQA\\IEDriverServer_x64_3.7.0\\IEDriverServer.exe"; System.setProperty("webdriver.ie.driver", service); InternetExplorerDriver 驱动程序 = 新 InternetExplorerDriver();

标签: selenium selenium-webdriver


【解决方案1】:

这会将美国添加为区域设置,然后等到显示医生的照片。

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] 的问题,那是什么?这个物体是从哪里来的?干杯
  • 从 Selenium ExecuteScript 页面检查定义 参数将通过“arguments”魔术变量提供给 JavaScript,就好像该函数是通过“Function.apply”调用的一样,并且 executeScript 的返回值是: 返回:Boolean、Long、String、List 或 WebElement 之一。或为空。这意味着返回的对象是一个列表,您可以通过 arguments[0] 魔术变量与之交互。
【解决方案2】:

加载页面后,首先我们需要wait 以使预期的WebElement 即第一个Search Box 可点击。然后我们将向Search Boxes 发送一些文本,然后在Search Button 上调用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一样
  • 我认为这与我的 Internet Explorer 版本和下载驱动程序有关,因为代码在 chrome 中运行良好
  • 查看我更新的答案,让我知道状态
【解决方案3】:
WebDriverWait wait =new WebDriverWait(driver, 90);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("xpath")));     
driver.findElement(By.xpath("xpath")).click();

【讨论】:

  • 请努力解释你的答案。
猜你喜欢
  • 2012-03-26
  • 1970-01-01
  • 2018-02-19
  • 1970-01-01
  • 2016-12-25
  • 1970-01-01
  • 2015-11-30
  • 2013-08-06
  • 2016-07-28
相关资源
最近更新 更多