【问题标题】:How to locate a complicated tag without id name or value?如何找到没有 id 名称或值的复杂标签?
【发布时间】:2019-04-11 15:12:11
【问题描述】:

你好,请帮助我找到这个的xpath

<button _ngcontent-c27="" aria-label="VALIDATE" color="primary" fxflex="" mat-raised-button="" type="submit" class="mat-raised-button mat-primary" style="flex: 1 1 0%; box-sizing: border-box;"><span class="mat-button-wrapper"> VALIDATE </span><div class="mat-button-ripple mat-ripple" matripple=""></div><div class="mat-button-focus-overlay"></div></button>

但是当我复制 xpath 时

/html/body/app-root/app-side-nav/mat-sidenav-container/mat-sidenav-content/main/app-otp/app-page-container/div/form/div/div/form/div/div[1]/button

然后当我使用它时,控制台错误 NoSuchElementException

这是我在 selenium 中的代码,谢谢

driver.findElement(By.xpath("/html/body/app-root/app-side-nav/mat-sidenav-container/mat-sidenav-content/main/app-otp/app-page-container/div/form/div/div/form/div/div[1]/button")).click();

HTML Button please click

【问题讨论】:

  • 你能粘贴一个简化版的 HTML 吗? (只是相关的标签,不需要包含文本等内容...)
  • 嗨@Isma,你能教我如何在 selenium webdriver 中新手谢谢
  • 我认为你需要发布你的html,否则很难知道发生了什么
  • 嗨@Isma我已经添加了html
  • 您的 html 不包含 &lt;button&gt;,动态内容?

标签: java selenium xpath css-selectors webdriverwait


【解决方案1】:

所需元素是 Angular 元素,因此要定位(并可能调用 click()),您必须为所需的 可点击元素诱导 WebDriverWait 和您可以使用以下任一解决方案:

  • cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.mat-raised-button.mat-primary[aria-label='VALIDATE']>span.mat-button-wrapper"))).click();
    
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='mat-raised-button mat-primary' and @aria-label='VALIDATE']/span[@class='mat-button-wrapper']"))).click();
    

更新

当您遇到错误...未知错误:元素无法点击时...您可以使用以下任一解决方案:

  • cssSelector:

    WebElement my_css_element = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.mat-raised-button.mat-primary[aria-label='VALIDATE']>span.mat-button-wrapper")));
    ((JavascriptExecutor)driver).executeScript("arguments[0].click();", my_css_element);
    
  • xpath:

    WebElement my_xpath_element = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='mat-raised-button mat-primary' and @aria-label='VALIDATE']/span[@class='mat-button-wrapper']")));
    ((JavascriptExecutor)driver).executeScript("arguments[0].click();", my_xpath_element);
    

参考

您可以在

中找到相关讨论

【讨论】:

  • 嗨@DebanjanB我使用你的解决方案我遇到了这个我该怎么办org.openqa.selenium.WebDriverException:未知错误:元素... 在点 (677, 432) 处不可点击
  • @ReneAlano 查看我的答案更新并告诉我状态。
  • @Hi DebanjanB 非常感谢!那么让我了解你刚刚做了什么?
  • @ReneAlano 查看我的答案更新,如果有任何问题,请告诉我。
【解决方案2】:

首先作为一种练习,最好使用相对 xpath,它会帮助您在添加新更改时重用您的代码。

其次,如果没有 ID 、 Name 或任何特定的元素定位器要处理,那么你必须尝试像

这样的属性
`//button[contains(text(),'Validate')]`

`//button[@type='submit']`

//button[@aria-label='VALIDATE']

【讨论】:

  • 嗨@SDK_90 它不工作控制台:没有这样的元素:无法找到元素:
  • 嗨@ReneAlano,可能有多种原因。延迟时间,脚本中的元素名称不正确(区分大小写),或者实际上元素在活动窗口区域内不可见但在页面内可见(您需要添加向下滚动)。等首先检查定位器名称
【解决方案3】:

我认为您正在尝试使基于 Angular 框架的网站自动化。在角度相同的 div 用于多种目的。开发人员只需更改绑定并将同一个按钮用于多种用途。

如果您共享完整的网页 html,那将是一个很大的帮助。

只需检查几件事可能会有所帮助:

  1. Web 元素在页面上可见。(css 属性如 display:none 使元素从页面中消失。它仍然存在于 DOM 中但 Web 驱动程序找不到它)

  2. 它不是从 jquery 或 ajax 加载的。如果是,那么也许你需要添加 webdriver 等等。

确切的解决方案取决于您的完整网页。

【讨论】:

    【解决方案4】:

    查找多个属性使用[@type=... and @class=...]

    //button[@type='submit' and @class='mat-raised-button mat-primary']
    

    【讨论】:

      【解决方案5】:

      希望这三个对你有帮助,

      //button[@type='submit']
      //*[contains(@type,'submit')]
      //button[contains(text(),'VALIDATE')]
      

      如果不尝试在此处发布 HTML 代码,这样会有所帮助,或者,请查看这篇文章自己尝试更多 - https://www.guru99.com/xpath-selenium.html

      【讨论】:

      • 嗨@koushick,它不起作用尝试阅读您发布的第一篇文章,谢谢!
      猜你喜欢
      • 1970-01-01
      • 2020-02-05
      • 1970-01-01
      • 1970-01-01
      • 2020-01-15
      • 2021-12-12
      • 2021-04-28
      • 2012-08-11
      • 1970-01-01
      相关资源
      最近更新 更多