Java 定位器有4个匹配节点,当我们有4个匹配元素时,如何在Selenium中编写xpath?

Java 定位器有4个匹配节点,当我们有4个匹配元素时,如何在Selenium中编写xpath?,java,css,selenium,xpath,Java,Css,Selenium,Xpath,我在Selenium中的代码: driver.findElement(By.xpath(“//a[@href=”/agent create profile'][1]”)。单击() 它给出错误:线程“main”org.openqa.selenium.NoSuchElementException中出现异常:没有这样的元素:无法找到元素:{“method”:“xpath”,“selector”:“//a[@href=”/agent create profile'][1]” 选择器集线器显示了4个与提供

我在Selenium中的代码:
driver.findElement(By.xpath(“//a[@href=”/agent create profile'][1]”)。单击()

它给出错误:线程“main”org.openqa.selenium.NoSuchElementException中出现异常:没有这样的元素:无法找到元素:{“method”:“xpath”,“selector”:“//a[@href=”/agent create profile'][1]”

选择器集线器显示了4个与提供的xpath匹配的元素:


<a tabindex="0" class="primary-button__PrimaryButton-iSIsNJ cjTSCX abstract-button abstract-button--size-large" href="/agent-create-profile" style="" xpath="2"> Create Profile </a>

<a tabindex="0" class="primary-button__PrimaryButton-iSIsNJ cjTSCX abstract-button abstract-button--size-large" href="/agent-create-profile" style="" xpath="3"> Let's start </a>

<a tabindex="0" class="primary-button__PrimaryButton-iSIsNJ cjTSCX abstract-button abstract-button--size-large" href="/agent-create-profile" style="" xpath="4"> Agent Sign up </a>

<a rel="noopener noreferrer" class="navigation-section__menu-link" href="/agent-create-profile" style="" xpath="4"></a>````


Below is the html code:
````<a tabindex="0" class="primary-button__PrimaryButton-iSIsNJ cjTSCX abstract-button abstract-button--size-large" href="/agent-create-profile" style="" xpath="1">Sign up</a>````

````
以下是html代码:
````````

我认为在xpath的开头和点击
[1]

试试这个:

driver.findElement(By.xpath("(//a[@href='/agent-create-profile'])[1]")).click()
如果您想要第一个元素,另一种选择是:

driver.findElement(By.xpath("//a[contains(text(),'Create Profile')]")).click()

由于
xpath
属性是唯一的:除了现有的答案之外,
xpath
还有一个选项:

driver.findElement(By.xpath("//a[@xpath='1']"));
css
非常相似:

driver.findElement(By.cssSelector("a[xpath='1']"));

请尝试使用-partialLinkText方法:

或者,您可以选择所有链接作为列表,并通过for循环进行迭代,并通过getText()方法匹配所需的链接文本获取。如果可能,请分享网站链接

List<WebElement> linkList = driver.findElements(By.xpath(“”));
for(WebElement link : linkList) {
   if (link.getText().contains(“create-profile”)) {
   link.click();
   break;
   }
}
List linkList=driver.findElements(By.xpath(“”);
用于(WebElement链接:链接列表){
if(link.getText()包含(“创建配置文件”)){
link.click();
打破
}
}

我将在您的基础上再添加一些选项。感谢您回复上述建议。我尝试了你的第一个建议,它给了我这个错误:线程“main”org.openqa.selenium.NoSuchElementException中的异常:没有这样的元素:找不到元素:{“method”:“xpath”,“selector”:“(//a[@href='/agent create profile'])[1]”。我还尝试了第二个建议:没有找到与//a[contains(text(),'create profile')匹配的元素所以我用driver.findelelement(By.xpath(“//a[contains(text(),'Sign up')]”)更新了相同的xpath。click()-它找到了一个匹配的节点,但是,它没有给我这样的错误:找不到元素:{“method”:“xpath”,“selector”:”//a[contains(text(),'Sign up')]”-很抱歉,我对自动化完全陌生,因此我很难调试myselfas@vitaliis提到的错误,我们需要查看更多周围的HTML代码才能提供帮助。可能有一个iframe或其他东西阻止了这些元素的查看谢谢回答,我尝试了你的建议并给出了错误:对于第一个答案,错误不是这样的元素:无法找到元素:{“method”:“xpath”,“selector”:“//a[@xpath='1']”,对于第二个答案,错误是没有这样的元素:无法定位元素:{“方法”:“css选择器”,“选择器”:“a[xpath='1']”将外部html代码添加到问题中。可能有多个usch元素具有指定的定位器。你只发布了其中的一部分。
List<WebElement> linkList = driver.findElements(By.xpath(“”));
for(WebElement link : linkList) {
   if (link.getText().contains(“create-profile”)) {
   link.click();
   break;
   }
}