如何解决无法使用带有Java客户端的Selenium Webdriver单击输入类型提交导航
打开http://www.canadapostsurvey.ca/,尝试找到“下一步”导航按钮,然后单击它,或使用xpath或cssselector或其他方法将其提交,但不起作用。 (更新:未使用任何find元素方法找到了替代解决方案,但希望听到其他人的意见)
尝试了不同的定位器属性,但没有一个起作用,有什么主意吗?
new WebDriverWait(driver,5).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"nav-controls\"]/input")));
driver.findElement(By.xpath("//*[@id=\"nav-controls\"]/input")).click();`
html代码:
<div id="nav-controls" class="btn-container nav-center">
<input type="submit" name="_NNext" class="mrNext" style="" value="Next" alt="Next">
</div>
页面截图:
解决方法
按钮在框架中,因此请先切换到框架(“ mainFrame”),然后单击按钮。
您可以使用下面的代码来实现相同的目的。
System.setProperty("webdriver.chrome.driver",System.getProperty("user.dir") + "\\src\\test\\resources\\executables\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.canadapostsurvey.ca/");
WebDriverWait wait = new WebDriverWait(driver,30);
driver.switchTo().frame("mainFrame");
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='nav-controls']/input")));
driver.findElement(By.xpath("//*[@id='nav-controls']/input")).click();;
driver.quit();
,
要在元素内的click()
中使用占位符作为网址https://mail.protonmail.com/create/new?language=en中的选择用户名,因为所需元素在<iframe>
中,因此您必须:
- 为所需的 frameToBeAvailableAndSwitchToIt 诱导 WebDriverWait 。
- 为所需的 elementToBeClickable 诱导 WebDriverWait 。
- 您可以使用以下任一Locator Strategies:
-
使用 cssSelector :
driver.get("http://www.canadapostsurvey.ca/"); new WebDriverWait(driver,10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("frame#mainFrame"))); new WebDriverWait(driver,10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input[value='Next']"))).click();
-
使用 xpath :
driver.get("http://www.canadapostsurvey.ca/"); new WebDriverWait(driver,10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//frame[@id='mainFrame']"))); new WebDriverWait(driver,10).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@value='Next']"))).click();
-
参考
您可以在以下位置找到一些相关的讨论
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。