简体   繁体   English

在Java中获取元素时构建Xpath

[英]Build Xpath when get element in java

I'm newbie in selenium webdriver and today i have some issues with my sample login and logout scenario. 我是Selenium Webdriver的新手,今天我的示例登录和注销方案存在一些问题。 I want to get element in gmail but eclipse always notice error. 我想在gmail中获取元素,但日食总是注意到错误。 Element i want to get (my email test) 我想要得到的元素(我的电子邮件测试)

And here is my xpath: //*[@id='gb']//descendant::div[@class='gb_vb'] . 这是我的xpath: //*[@id='gb']//descendant::div[@class='gb_vb'] But when find it, eclipse always notice error. 但是,当找到它时,蚀总是会注意到错误。

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)

Are you opening the menu that the element is contained in first? 您要先打开包含该元素的菜单吗? I've just tried this, and it will only work if you open the containing menu before trying to find the element containing your email address beforehand eg (C# for the example): 我已经尝试过了,它只有在您尝试先查找包含您的电子邮件地址的元素之前打开包含菜单(例如,C#)时才有效:

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

Then use your code above to find the element you're looking for. 然后,使用上面的代码查找所需的元素。

试试这个XPath

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

In that case, you should use Explicit Waits 在这种情况下,您应该使用“ 显式等待”

Cause maybe web browser need time to load all element (for example: lazy load, load more, ajax ...) 原因可能是网络浏览器需要时间来加载所有元素(例如:延迟加载,加载更多,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']"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM