Selenium无法通过id找到元素

时间:2017-07-06 14:51:20

标签: java selenium

我试图用Selenium创建一个简单的测试。我之前使用过这段代码而且运行正常。不知何故,当我换到新机器时它停止工作。我尝试添加一些代码,等到元素可见,但它没有成功。

我只是尝试通过其id找到一个元素,移动到该元素并单击它。不知何故,代码根本找不到元素。

这是我的代码:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class SeleniumBasicTest {

  public static void main(String[] args) throws InterruptedException {

    DesiredCapabilities caps = DesiredCapabilities.internetExplorer();
    caps.setCapability("ignoreZoomSetting", true);
    WebDriver driver = new InternetExplorerDriver(caps);

    driver.get("http://www.store.demoqa.com");

    WebDriverWait wait = new WebDriverWait(driver, 20);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("menu-item-33")));

    System.out.println("Successfully opened the website www.Store.Demoqa.com");

    WebElement producCategorytMenu = driver.findElement(By.id("menu-item-33"));

    Actions builder = new Actions(driver);
    builder.moveToElement(producCategorytMenu).perform();
    builder.click(producCategorytMenu).perform();

    Thread.sleep(5000);

    System.out.println("quiting test...");
    driver.quit();
  }

}

这是我收到的例外情况:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to find element with id == menu-item-33 (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 315 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.3.0', revision: 'b526bd5b41', time: '2017-03-07 19:26:04 +0000'
System info: host: 'CMTCLX62137', ip: '53.19.245.167', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_31'
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
Capabilities [{se:ieOptions={browserAttachTimeout=0, ie.enableFullPageScreenshot=true, enablePersistentHover=true, ie.forceCreateProcessApi=false, ie.forceShellWindowsApi=false, ignoreZoomSetting=true, ie.fileUploadDialogTimeout=3000, ie.useLegacyFileUploadDialogHandling=false, nativeEvents=true, ie.ensureCleanSession=false, elementScrollBehavior=0, ie.browserCommandLineSwitches=, requireWindowFocus=false, initialBrowserUrl=http://localhost:37146/, ignoreProtectedModeSettings=false, enableElementCacheCleanup=true}, browserName=internet explorer, pageLoadStrategy=normal, javascriptEnabled=true, version=11, platform=WINDOWS, unexpectedAlertBehaviour=dismiss}]
Session ID: 1e1740a9-00a2-4b92-814a-1979cfcef8e0
*** Element info: {Using=id, value=menu-item-33}
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:216)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:168)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:638)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:371)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:420)
    at org.openqa.selenium.By$ById.findElement(By.java:218)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:363)
    at com.mytest.SeleniumBasicTest.main(SeleniumBasicTest.java:22)

以下是包含菜单的HTML部分:

    <nav class="group" id="main-nav">
            <ul id="menu-main-menu" class="menu"><li id="menu-item-15" class="menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-11 current_page_item menu-item-15"><span class="before">&nbsp;</span><a href="http://store.demoqa.com/"><span></span>Home</a></li>
<li id="menu-item-33" class="menu-item menu-item-type-taxonomy menu-item-object-wpsc_product_category menu-item-has-children menu-item-33 has_children"><span class="before">&nbsp;</span><a href="http://store.demoqa.com/products-page/product-category/"><span></span>Product Category</a>
<ul class="sub-menu">
    <li id="menu-item-34" class="menu-item menu-item-type-taxonomy menu-item-object-wpsc_product_category menu-item-34"><span class="before">&nbsp;</span><a href="http://store.demoqa.com/products-page/product-category/accessories/"><span></span>Accessories</a></li>
    <li id="menu-item-35" class="menu-item menu-item-type-taxonomy menu-item-object-wpsc_product_category menu-item-35"><span class="before">&nbsp;</span><a href="http://store.demoqa.com/products-page/product-category/imacs/"><span></span>iMacs</a></li>
    <li id="menu-item-36" class="menu-item menu-item-type-taxonomy menu-item-object-wpsc_product_category menu-item-36"><span class="before">&nbsp;</span><a href="http://store.demoqa.com/products-page/product-category/ipads/"><span></span>iPads</a></li>
    <li id="menu-item-37" class="menu-item menu-item-type-taxonomy menu-item-object-wpsc_product_category menu-item-37"><span class="before">&nbsp;</span><a href="http://store.demoqa.com/products-page/product-category/iphones/"><span></span>iPhones</a></li>
    <li id="menu-item-38" class="menu-item menu-item-type-taxonomy menu-item-object-wpsc_product_category menu-item-38"><span class="before">&nbsp;</span><a href="http://store.demoqa.com/products-page/product-category/ipods/"><span></span>iPods</a></li>
    <li id="menu-item-39" class="menu-item menu-item-type-taxonomy menu-item-object-wpsc_product_category menu-item-39"><span class="before">&nbsp;</span><a href="http://store.demoqa.com/products-page/product-category/macbooks/"><span></span>MacBooks</a></li>
</ul>
</li>
<li id="menu-item-16" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-16 has_children"><span class="before">&nbsp;</span><a><span></span>#0 (no title)</a>
<ul class="sub-menu">
    <li id="menu-item-17" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-17"><span class="before">&nbsp;</span><a><span></span>#0 (no title)</a></li>
    <li id="menu-item-18" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-18"><span class="before">&nbsp;</span><a><span></span>#0 (no title)</a></li>
</ul>
</li>
<li id="menu-item-72" class="menu-item menu-item-type-taxonomy menu-item-object-wpsc_product_category menu-item-72"><span class="before">&nbsp;</span><a href="http://store.demoqa.com/products-page/product-category/"><span></span>All Product</a></li>
</ul> 

来源:http://store.demoqa.com/

任何人都知道可能会发生什么? 我已经在StackOverflow上阅读了所有类似的问题,尝试了每一个选项,但它仍然没有找到明显存在的元素。

3 个答案:

答案 0 :(得分:1)

以下是您的问题的答案:

我在代码块中没有看到任何错误。但是,当您诱导visibilityOfElementLocated时,您可以考虑使用ExplicitWait方法而不是li。此外,您可以考虑找到元素的id标记,而不是将menu-item-33元素与a定位为a。最后,Java代码会收到正常的Actions点击,您无需使用 import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.ie.InternetExplorerDriver; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; public class Q44952318 { public static void main(String[] args) { System.setProperty("webdriver.ie.driver","C:\\Utility\\BrowserDrivers\\IEDriverServer_32.exe"); DesiredCapabilities cap = DesiredCapabilities.internetExplorer(); cap.setCapability("ignoreZoomSetting", true); cap.setCapability("platform", "WIN8"); cap.setCapability("version", "11"); cap.setCapability("browserName", "internet explorer"); cap.setCapability("ignoreProtectedModeSettings",1); cap.setCapability("nativeEvents","false"); cap.setCapability("requireWindowFocus","true"); cap.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true); WebDriver driver = new InternetExplorerDriver(cap); driver.get("http://www.store.demoqa.com"); System.out.println("Successfully opened the website www.Store.Demoqa.com"); WebDriverWait wait = new WebDriverWait(driver, 20); WebElement product = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//li[@id='menu-item-33']/a"))); product.click(); System.out.println("quiting test..."); driver.quit(); } } 课程。这是你自己的代码,有一些简单的调整:

model.appetizerMenu

如果这回答你的问题,请告诉我。

答案 1 :(得分:0)

您的代码在我的计算机上运行良好。所以我的猜测是IE配置中有一些不同之处。正如您所说,当您切换计算机时,问题已经开始。这意味着您可能未配置当前的IE。如果您可以发布当前的IE版本,也会很棒。

当您使用IE时,需要先设置一些设置,然后才能对其进行测试。

  1. 你有&#34;增强保护模式&#34;在IE设置中禁用?它位于高级选项卡下并处于安全状态。它必须被禁用
  2. 您必须将每个区域的保护模式设置设置为相同的值。 为所有4个区域(Int​​ernet,本地Intranet,受信任的站点,受限制的站点)启用保护模式enter image description here

答案 2 :(得分:0)

我刚刚发现我的问题是由IE的保护模式未在所有区域设置为相同选项引起的。使用以下方法忽略此配置无效:

  DesiredCapabilities cap = DesiredCapabilities.internetExplorer();
  cap.setCapability("ignoreProtectedModeSettings",true);
  cap.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
  WebDriver driver = new InternetExplorerDriver(cap);

我在代码工作的另一台计算机上更改了它,并且在问题中描述了相同的问题。改回来又恢复了工作。

因此,似乎没有办法超越代码上的保护模式配置。