无法在Eclipse中使用xpath定位元素

时间:2017-02-15 09:49:26

标签: xpath

我使用firepath归因于找到元素并获取xpath,但在eclipse中,它不起作用。还尝试了其他引用http://www.guru99.com/xpath-selenium.html的变体,这些变体也适用于firepath,但是eclipse他们没有发现它们就像示例1

代码片段:

<span class="registration ng-scope" translate="navbar_component.menuItems.sign_up" ng-click="$ctrl.goToPage('registration')" role="button" tabindex="0">Sign up</span>
<span class="registration ng-scope" translate="navbar_component.menuItems.sign_in" ng-click="$ctrl.goToPage('sign-in')" role="button" tabindex="0">Sign in</span>

这就是我从firepath得到的:

driver.findElement(By.xpath(".//*[@id='sign-in']/span[2]/span[2]")).click();

(那是火道给我的)

我的xpath示例 1

 .//*[@class='registration ng-scope']//following-sibling::span

如果您能告诉我可以用来查找"Sign in"按钮的内容,我将非常感激。

收到的例外:

Exception in thread "main" org.openqa.selenium.ElementNotVisibleException:  Build info: version: '3.0.1', revision: '1969d75', time: '2016-10-18 09:49:13 -0700' System info: host: 'WS250', ip: '192.168.84.165', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_121' Driver info: org.openqa.selenium.firefox.FirefoxDriver Capabilities [{rotatable=false, raisesAccessibilityExceptions=false, marionette=true, firefoxOptions={args=[], prefs={}}, appBuildId=20170125094131, version=, platform=XP, proxy={}, command_id=1, specificationLevel=0, acceptSslCerts=false, processId=2108, browserVersion=51.0.1, platformVersion=10.0, XULappId={ec8030f7-c20a-464f-9b0e-13a3a9e97384}, browserName=firefox, takesScreenshot=true, takesElementScreenshot=true, platformName=windows_nt}] Session ID: 14605620-ee13-4766-8edf-7b6cf31b35f6
    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.RemoteWebElement.execute(RemoteWebElement.java:274)
    at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:84)
    at Selenium.test.SignIN.main(SignIN.java:14)

1 个答案:

答案 0 :(得分:1)

您可以简单地将目标元素的文本内容用作:

driver.findElement(By.xpath("//span[text()='Sign in']")).click();

您可能需要等到元素可见:

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[text()='Sign in']")).click();