【问题标题】:Build Xpath when get element in java在java中获取元素时构建Xpath
【发布时间】:2017-05-04 16:19:39
【问题描述】:

我是 selenium webdriver 的新手,今天我的示例登录和注销场景遇到了一些问题。我想在 gmail 中获取元素,但 eclipse 总是注意到错误。 Element i want to get (my email test)

这是我的 xpath://*[@id='gb']//descendant::div[@class='gb_vb'] 。 但是当找到它时,eclipse总是提示错误。

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: //*[@id='gb']//descendant::div[@class='gb_vb']
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.0.1', revision: '1969d75', time: '2016-10-18 09:49:13 -0700'
System info: host: 'Alex', ip: '192.168.1.7', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_112'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{rotatable=false, raisesAccessibilityExceptions=false, marionette=true, firefoxOptions={args=[], prefs={}}, appBuildId=20160623154057, version=, platform=XP, proxy={}, command_id=1, specificationLevel=0, acceptSslCerts=false, browserVersion=47.0.1, platformVersion=10.0, XULappId={ec8030f7-c20a-464f-9b0e-13a3a9e97384}, browserName=Firefox, takesScreenshot=true, takesElementScreenshot=true, platformName=Windows_NT, device=desktop}]
Session ID: 2fd78092-bf4a-4095-8a32-1a2e1d22ed3e
*** Element info: {Using=xpath, value=//*[@id='gb']//descendant::div[@class='gb_vb']}
    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.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:127)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:93)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:42)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:163)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:368)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:473)
    at org.openqa.selenium.By$ByXPath.findElement(By.java:361)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:360)
    at CucumberTest.LoginTestFunction.WaitandElement.findE(WaitandElement.java:19)
    at CucumberTest.LoginTestFunction.SeleniumTest.main(SeleniumTest.java:30)

【问题讨论】:

  • 添加目标元素的HTML而不是其图像
  • @Andersson 抱歉,但我不知道如何在 stackoverflow 中添加图片。我是这里的新手。我尝试使用 但只显示链接。
  • 不添加图片,添加HTML作为文本(从开发者控制台复制)

标签: java selenium xpath selenium-webdriver element


【解决方案1】:

您是否首先打开包含该元素的菜单?我刚刚尝试过,只有在尝试预先找到包含您的电子邮件地址的元素之前打开包含菜单,它才会起作用,例如(以 C# 为例):

IWebElement popupMenu = driver.FindElement(By.XPath("//*[@id=\"gb\"]/div[1]/div[1]/div[2]/div[4]/div[1]/a/span"));
popupMenu.Click();

然后使用上面的代码找到您要查找的元素。

【讨论】:

  • 你说得对,需要先显示此菜单,然后再获取文本。但我发现了关于显式等待的问题。
【解决方案2】:

试试这个XPath:

//div[@class="gb_vb"][contains(text(), "@gmail.com")]

【讨论】:

    【解决方案3】:

    在这种情况下,您应该使用Explicit Waits

    因为网页浏览器可能需要时间来加载所有元素(例如:延迟加载、加载更多、ajax ...)

    //wait for 10 seconds
    WebDriverWait logWait = new WebDriverWait(driver, 10);
    logWait.until(ExpectedConditions.presenceOfElementLocated(by));  
    //now you can find element
    driver.findElement(By.xpath("//*[@id='gb']//descendant::div[@class='gb_vb']"));
    

    【讨论】:

    • 感谢您提供有用的信息,尝试并成功
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-13
    • 1970-01-01
    • 2013-05-19
    • 2013-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多