Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在selenium中为登录链接编写xpath?_Java_Selenium Webdriver_Selenium Chromedriver - Fatal编程技术网

Java 如何在selenium中为登录链接编写xpath?

Java 如何在selenium中为登录链接编写xpath?,java,selenium-webdriver,selenium-chromedriver,Java,Selenium Webdriver,Selenium Chromedriver,请在xpath中为我提供此..的解决方案 driver.findElement( By.xpath("//a[contains(@href,'/MIS/PreLog/UserLogin.aspx') and @id='login']" ).click(); 我没有看到任何@id属性 另外,为什么要特别使用xpath?您可以使用driver.findElement(By.LinkText(“Login”)) 编辑1:试试这个 driver.findElement( By.xpath("

请在xpath中为我提供此..

的解决方案

driver.findElement( By.xpath("//a[contains(@href,'/MIS/PreLog/UserLogin.aspx') and  @id='login']" ).click();    
我没有看到任何
@id
属性

另外,为什么要特别使用xpath?您可以使用
driver.findElement(By.LinkText(“Login”))

编辑1:试试这个

driver.findElement( By.xpath("//a[contains(@href,'/MIS/PreLog/UserLogin.aspx') and  @id='login']" ).click(); 

您的代码中的定位器可以修改并按如下所示编写,请检查此操作是否有效

driver.findElement( By.xpath("//*[contains(@href,'UserLogin') and text()='Login']" )).click();

我的页面有两个登录文本,这就是为什么它不工作的原因。。请建议使用xpath。请参阅我的编辑@kripindas选择器缺少一个结束括号。@demouser123感谢您指出这个问题。我现在已经编辑过了。我没有在编辑器中尝试,因此错过了结尾括号。它给出了错误:-方法click()对于类型byThank@kripindas未定义,还请解释为什么我们使用*after/..???@bharat如果它有效,请接受答案。如果提到*,它将从节点获取。如果未在“/”之前使用*,则xpath定位器将不完整且无法工作。最常见的类名或href将在div或a之后开始(在您的情况下),因此为了找到href,我们必须在反斜杠之前使用*。您也可以使用a代替*,它也可以工作,但是如果a被更改为任何其他,它将不工作。因此,最好使用*。我尝试过这个解决方案,但只有一次它不起作用。请建议怎么做。?@bharat我在定位器中遗漏了一个结束括号。这就是它显示错误的原因。我想关于*的疑问现在已经消除了。欢迎来到SO,请提供HTML,也许有更简单的解决方案。
driver.findElement( By.xpath("//*[contains(@href,'UserLogin') and text()='Login']" )).click();
driver.findElement( By.xpath("//*[contains(@href,'UserLogin') and text()='Login']" )).click();