【问题标题】:Locators has 4 matching nodes, how to write xpath in Selenium when we have 4 elements matching?定位器有 4 个匹配节点,当我们有 4 个元素匹配时,如何在 Selenium 中编写 xpath?
【发布时间】:2021-08-05 14:52:46
【问题描述】:

我在 Selenium 中的代码: driver.findElement(By.xpath("//a[@href='/agent-create-profile'][1]")).click();

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

Selectors hub 显示 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>````

【问题讨论】:

    标签: java css selenium xpath


    【解决方案1】:

    我认为您在 xpath 的开头和点击 [1] 之前缺少一组括号

    试试这个:

    driver.findElement(By.xpath("(//a[@href='/agent-create-profile'])[1]")).click()
    

    如果你想要第一个元素,另一种选择:

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

    【讨论】:

    • 我会再为您添加一些选项。
    • 感谢您回复上述建议。我尝试了你的第一个建议,它给了我这个错误:线程“main”中的异常 org.openqa.selenium.NoSuchElementException:没有这样的元素:无法找到元素:{“method”:“xpath”,“selector” :"(//a[@href='/agent-create-profile'])[1]"}
    • 我也尝试了第二个建议:它没有找到与 //a[contains(text(),'Create Profile')] 匹配的元素,所以我更新了相同的xpath 与 driver.findElement(By.xpath("//a[contains(text(),'Sign up')]")).click() - 它找到 1 个匹配节点但是,它给了我错误no such element: Unable to locate element: {"method":"xpath","selector":"//a[contains(text(),'Sign up')]"} - 对不起,我对自动化完全陌生,所以我很难自己调试错误
    • 正如@vitaliis 提到的,我们需要查看更多周围的HTML 代码才能提供帮助。可能有 iframe 或其他东西阻挡了这些元素的视图
    【解决方案2】:

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

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

    css很相似:

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

    【讨论】:

    • 感谢您的回答,我尝试了您的建议及其给我的错误:对于第一个答案,错误是 no such element: Unable to locate element: {"method":"xpath ","selector":"//a[@xpath='1']"} 对于第二个答案,错误是 no such element: Unable to locate element: {"method":" css 选择器","选择器":"a[xpath='1']"}
    • 将外部 html 代码添加到您的问题中。也许还有更多带有指定定位器的 usch 元素。您只发布了其中的一部分。
    【解决方案3】:

    请尝试使用方法- partialLinkText:

     driver.findElement(By.partialLinkText("Create Profile")).click();
    

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

    List<WebElement> linkList = driver.findElements(By.xpath(“”));
    for(WebElement link : linkList) {
       if (link.getText().contains(“create-profile”)) {
       link.click();
       break;
       }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-15
      • 1970-01-01
      • 1970-01-01
      • 2020-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多