嗨,请帮助我必须找到它的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
这是我用硒编写的代码,感谢advace
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();
答案 0 :(得分:1)
我希望这三个方面有帮助
//button[@type='submit']
//*[contains(@type,'submit')]
//button[contains(text(),'VALIDATE')]
如果不尝试在此处发布HTML代码,那么它会有所帮助,或者,请参阅本文,对自己进行更多尝试-https://www.guru99.com/xpath-selenium.html
答案 1 :(得分:1)
使用多个属性查找[@type=... and @class=...]
//button[@type='submit' and @class='mat-raised-button mat-primary']
答案 2 :(得分:0)
我认为您正在尝试使基于角度框架的网站自动化。在角度上,相同的div用于多种用途。开发人员只需更改绑定并将同一按钮用于多种用途即可。
如果您共享完整的html网页,那将是一个很大的帮助。
只需检查几件事可能会有帮助:
Web元素在页面上可见。(css属性如display:none会使元素从页面上消失。它仍然存在于DOM中,但Web驱动程序找不到它)
它不是从jquery或ajax加载的。如果是,那么也许您需要添加webdriver等待。
确切的解决方案取决于您的完整网页。
答案 3 :(得分:0)
首先,最好使用相对xpath,这将在添加新更改时帮助您重用代码。
第二,如果没有ID,Name或任何要处理的特定元素定位符,则必须尝试使用
这样的属性`//button[contains(text(),'Validate')]`
`//button[@type='submit']`
//button[@aria-label='VALIDATE']
等
答案 4 :(得分:0)
所需元素是Angular元素,因此要定位(并可能调用click()
),您必须诱使 WebDriverWait 使所需元素变为可点击< / em>,则可以使用以下任一解决方案:
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);
您可以在
中找到相关的讨论