在 Selenium Java 中查找元素的 Xpath
Find Xpath for element in Selenium Java
我正在尝试使用此 XPATH 在 selenium 中查找元素。我在 firefox 网络浏览器中得到这个。
/html/body/div[5]/div[2]/div[9]/div[1]/div[2]/div/div[2]/div[2]/div/div/div[1]/div[2]/div[1]/a
HTML代码
<a href="/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&cad=rja&uact=8&ved=2ahUKEwiw7cbBv6LqAhVMAHIKHbbFCUYQFjAAegQIBxAB&url=https%3A%2F%2Fwww.google.com%2Fgmail%2F&usg=AOvVaw3mZ_qbD_gQyp_sqkjrwStn" onmousedown="return rwt(this,'','','','','AOvVaw3mZ_qbD_gQyp_sqkjrwStn','','2ahUKEwiw7cbBv6LqAhVMAHIKHbbFCUYQFjAAegQIBxAB','','',event)" data-ctbtn="2" data-cthref="/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&cad=rja&uact=8&ved=2ahUKEwiw7cbBv6LqAhVMAHIKHbbFCUYQFjAAegQIBxAB&url=https%3A%2F%2Fwww.google.com%2Fgmail%2F&usg=AOvVaw3mZ_qbD_gQyp_sqkjrwStn"><br><h3 class="LC20lb DKV0Md">Gmail by Google</h3><div class="TbwUpd NJjxre"><cite class="iUh30 bc tjvcx">www.google.com<span class="eipWBe"> › gmail</span></cite></div></a>
我的硒代码
driver.findElement(By.xpath("/html/body/div[5]/div[2]/div[9]/div[1]/div[2]/div/div[2]/div[2]/div/div/div[1]/div[2]/div[1]/a")).click();
driver.findElement(By.linkText("Sign in")).click();
但它不起作用。帮帮我。
因为元素是 Angular element to click on it, you need to use for the elementToBeClickable()
and you can use either of the following :
cssSelector
:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a[@href='https://www.google.com/gmail/'] h3"))).click();
xpath
:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//h3[text()='Gmail by Google']"))).click();
我正在尝试使用此 XPATH 在 selenium 中查找元素。我在 firefox 网络浏览器中得到这个。
/html/body/div[5]/div[2]/div[9]/div[1]/div[2]/div/div[2]/div[2]/div/div/div[1]/div[2]/div[1]/a
HTML代码
<a href="/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&cad=rja&uact=8&ved=2ahUKEwiw7cbBv6LqAhVMAHIKHbbFCUYQFjAAegQIBxAB&url=https%3A%2F%2Fwww.google.com%2Fgmail%2F&usg=AOvVaw3mZ_qbD_gQyp_sqkjrwStn" onmousedown="return rwt(this,'','','','','AOvVaw3mZ_qbD_gQyp_sqkjrwStn','','2ahUKEwiw7cbBv6LqAhVMAHIKHbbFCUYQFjAAegQIBxAB','','',event)" data-ctbtn="2" data-cthref="/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&cad=rja&uact=8&ved=2ahUKEwiw7cbBv6LqAhVMAHIKHbbFCUYQFjAAegQIBxAB&url=https%3A%2F%2Fwww.google.com%2Fgmail%2F&usg=AOvVaw3mZ_qbD_gQyp_sqkjrwStn"><br><h3 class="LC20lb DKV0Md">Gmail by Google</h3><div class="TbwUpd NJjxre"><cite class="iUh30 bc tjvcx">www.google.com<span class="eipWBe"> › gmail</span></cite></div></a>
我的硒代码
driver.findElement(By.xpath("/html/body/div[5]/div[2]/div[9]/div[1]/div[2]/div/div[2]/div[2]/div/div/div[1]/div[2]/div[1]/a")).click();
driver.findElement(By.linkText("Sign in")).click();
但它不起作用。帮帮我。
因为元素是 Angular element to click on it, you need to use elementToBeClickable()
and you can use either of the following
cssSelector
:new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a[@href='https://www.google.com/gmail/'] h3"))).click();
xpath
:new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//h3[text()='Gmail by Google']"))).click();