[英]Java Selenium - How to click on a button link element with no ID?
I've been trying to get the WebDriver to find the element by xpath and using the href link directly behind the button, however to no avail.我一直试图让 WebDriver 通过 xpath 找到元素,并直接使用按钮后面的 href 链接,但无济于事。 Here's my code so far:
到目前为止,这是我的代码:
package mavensample;
import.java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class mavensample {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "chromedriverpath";
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().pageLoadTimeout(40, Timeunit.SECONDS);
driver.manage().timeouts().implicitlyWait(40, Timeunit.SECONDS);
driver.get("https://initialLink.net/");
driver.findElement(By.cssSelector("a[href='https://censoredlink.net/')]"));
}}
This is the elemnt info I get on inspect: a href= "https://censoredlink.net/" class="btn btn-sm btn-light mb-1" target="_blank">QA这是我在检查时获得的元素信息: a href="https://censoredlink.net/" class="btn btn-sm btn-light mb-1" target="_blank">QA
I would normally locate the element through text, however there are multiple different QA buttons for different environments.我通常会通过文本定位元素,但是对于不同的环境有多个不同的 QA 按钮。 I tried using CssSelector and just inputting the link directly however that didn't work either.
我尝试使用 CssSelector 并直接输入链接,但这也不起作用。
Any help is much appreciated!任何帮助深表感谢!
In case https://censoredlink.net/
is unique value for href
attribute you can try the following XPath:如果
https://censoredlink.net/
是href
属性的唯一值,您可以尝试以下 XPath:
driver.findElement(By.xpath("//a[@href='https://censoredlink.net/']"))
Or with CssSelector或使用 CssSelector
driver.findElement(By.cssSelector("a[href='https://censoredlink.net/']"))
UPD UPD
I guess you are missing a wait.我猜你错过了等待。 I mean you need to wait for the element to be loaded before accessing it.
我的意思是你需要等待元素被加载才能访问它。 The best way to do that is to use
WebDriverWait
.最好的方法是使用
WebDriverWait
。
Also it's possible the element is inside iframe etc.也有可能该元素位于 iframe 等内部。
We need to see that page...我们需要看到那个页面......
There is a typo error in your css selector
.您的
css selector
中存在拼写错误。 you need to remove )
this你需要删除
)
这个
Instead of this而不是这个
driver.findElement(By.cssSelector("a[href='https://censoredlink.net/')]"));
It should have been应该是
driver.findElement(By.cssSelector("a[href='https://censoredlink.net/']"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.