Java 如何使用 Selenium 单击 href 链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30635228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 09:54:45  来源:igfitidea点击:

How to click a href link using Selenium

javaseleniumxpath

提问by Psl

I have a htmlhreflink

我有一个htmlhref链接

<a href="/docs/configuration">App Configuration</a>

using Selenium I need to click the link. Currently, I am using below code -

使用 Selenium 我需要点击链接。目前,我正在使用以下代码 -

Driver.findElement(By.xpath("//a[text()='App Configuration']")).click(); 

But it's not redirecting to the page. I also tried below code -

但它没有重定向到页面。我也试过下面的代码 -

 Driver.findElement(By.xpath(//a[@href ='/docs/configuration']")).click();

But this is throwing below exception -

但这引发了以下异常-

org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with Command duration or timeout: 13 milliseconds

org.openqa.selenium.ElementNotVisibleException:元素当前不可见,因此可能无法与命令持续时间或超时交互:13 毫秒

The link is visible and page is completely loaded. I don't know what's wrong with my code.

链接可见且页面已完全加载。我不知道我的代码有什么问题。

回答by Arjit

Use

driver.findElement(By.linkText("App Configuration")).click()

Other Approaches will be

其他方法将是

JavascriptLibrary jsLib = new JavascriptLibrary(); 
jsLib.callEmbeddedSelenium(selenium, "triggerMouseEventAt", elementToClick,"click", "0,0");

or

或者

((JavascriptExecutor) driver).executeScript("arguments[0].click();", elementToClick);

For detailed answer, View this post

详细解答,查看此贴

回答by Saifur

Seems like the atag is hidden. Remember Selenium is not able to interact with hidden element. Javascriptis the only option in that case.

好像a标签被隐藏了。请记住 Selenium 无法与隐藏元素进行交互。Javascript在这种情况下是唯一的选择。

By css = By.cssSelector("a[href='/docs/configuration']");
WebElement element = driver.findElement(css);
((JavascriptExecutor)driver).executeScript("arguments[0].click();" , element);

回答by Saritha G

 webDriver.findElement(By.xpath("//a[@href='/docs/configuration']")).click();

The above line works fine. Please remove the space after href.

上面的行工作正常。请删除href 后的空格。

Is that element is visible in the page, if the element is not visible please scroll down the page then perform click action.

该元素在页面中是否可见,如果该元素不可见,请向下滚动页面然后执行点击操作。

回答by Nitinkumar

You can use this method:

您可以使用此方法:

For the links if you use linkText();it is more effective than the any other locator.

对于链接,如果您使用linkText();它,它比任何其他定位器都更有效。

driver.findElement(By.linkText("App Configuration")).click();

回答by omkar mahajan

You can use xpathas follows, try this one :

你可以使用xpath如下,试试这个:

driver.findElement(By.xpath("(.//[@href='/docs/configuration'])")).click();

回答by Juhil Somaiya

Use an explicit waitfor the element like this:

wait对元素使用显式,如下所示:

WebDriverWait wait1 = new WebDriverWait(driver, 500);
wait1.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("path of element"))).click();

回答by user11905979

How to click on link without using click method in selenum?

如何在不使用 selenum 中的 click 方法的情况下单击链接?

This is a tricky question. Follow the below steps:

这是一个棘手的问题。请按照以下步骤操作:

driver.get("https://www.google.com");
String gmaillink= driver.findElement(By.xpath("//a[@href='https://mail.google.com/mail/?tab=wm&ogbl']")).getAttribute("href");
System.out.println(gmaillink);
driver.get(gmaillink);

回答by Salman Arshad

Try to use Action class to reach the element

尝试使用 Action 类到达元素

Actions action = new Actions(driver);
action.MoveToElement(driver.findElement(By.xpath("//a[text()='AppConfiguration']")));
action.Perform();

回答by evan

Thi is your code :

这是你的代码:

Driver.findElement(By.xpath(//a[@href ='/docs/configuration']")).click();

You missed the Quotation mark

你错过了引号

it should be as below

它应该如下

Driver.findElement(By.xpath("//a[@href='/docs/configuration']")).click();

Hope this helps!

希望这可以帮助!