如何定位没有 ID 名称或值的复杂标签?

How to locate a complicated tag without id name or value?

嗨,请帮我找到这个的 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

我希望这三个帮助,

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

如果不尝试 post 此处的 HTML 代码,它会有所帮助,或者,查看本文,自己尝试更多 - https://www.guru99.com/xpath-selenium.html

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

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

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

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

只需检查一些可能有帮助的事情:

  1. Web 元素在页面上可见。(css 类似 display:none 的属性使元素从 page.It 中消失,但仍然存在于 DOM 中driver找不到)

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

具体的解决方案取决于您的完整网页。

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

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

之类的属性
`//button[contains(text(),'Validate')]`

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

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

所需的元素是一个 Angular 元素,因此要定位(并可能调用 click())您必须为所需的 [=39] 引入 WebDriverWait =]element To Be Clickable,您可以使用以下任一解决方案:

  • 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);
    

参考

您可以在

中找到相关讨论
  • Selenium Web Driver & Java. Element is not clickable at point (36, 72). Other element would receive the click: