在IE 11中未选择Selenium xpath

时间:2017-11-09 06:00:43

标签: selenium xpath

由于我来自测试背景,并且我对编码知识非常少,因此我的应用程序自动化面临一个小问题。我无法在IE11中获取该元素的xpath。请找到以下详细信息: -

the IE content screenshot

我尝试的代码:

driver.findElement(By.xpath("//*[@id='WD91']")).click();

driver.findElement(By.xpath("//a[.='Manage Device']")).click();

但是我在尝试运行时遇到以下错误: -

Started InternetExplorerDriver server (32-bit)
3.6.0.0
Listening on port 42278
Only local connections are allowed
Nov 09, 2017 6:39:27 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to find element with xpath == //*[@id='WD91']
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.7.0', revision: '2321c73', time: '2017-11-/

有人能告诉我,如果我做错了吗? 如果我的问题重复或愚蠢,请提前道歉。我会感谢你的帮助。谢谢

2 个答案:

答案 0 :(得分:0)

似乎你的xpath是正确的,但是尝试使用ID并且还包括等待(如果需要)

WebDriverWait wait=new WebDriverWait(driver, 30);
WebElement ElementBtn= driver.findElement(By.id("WD91"));
wait.until(ExpectedConditions.elementToBeClickable(ElementBtn));
ElementBtn.click();

使用xpath

WebDriverWait wait=new WebDriverWait(driver, 30);
WebElement ElementBtn=driver.findElement(By.xpath("//*[@id='WD91']"));
wait.until(ExpectedConditions.elementToBeClickable(ElementBtn));
ElementBtn.click();

或尝试使用JavascriptExecutor

WebElement ElementBtn= driver.findElement(By.id("WD91"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", ElementBtn);

答案 1 :(得分:0)

根据您分享的HTML,我们需要尝试点击<a>标记,如下所示:

driver.findElement(By.xpath("//a[@id='WD91' and @class='urLnkFunction urTxtStd']")).click();

您还可以尝试点击<img>标记,如下所示:

driver.findElement(By.xpath("//a[@id='WD91' and @class='urLnkFunction urTxtStd']/img[contains(@src,'//www.myhomemsn.com/')]")).click();