如何使用Selenium Java标记超链接

huangapple go评论82阅读模式
英文:

How to mark a Hyperlink with Selenium Java

问题

我试图标记此网站上的超链接 "Edit this page":https://www.selenium.dev/documentation/en/getting_started/quick/

我的代码如下:

driver.get("https://www.selenium.dev/documentation/en/getting_started/quick/");
WebElement elem = driver.findElement(By.linkText("Edit this page"));
Actions actions = new Actions(driver);
Thread.sleep(3000);
actions.moveToElement(elem)
.click()
.keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL)
.build().perform();

使用这段代码会标记页面上的所有文本。如何只标记超链接?

英文:

I'm trying to mark the hyperlink "Edit this page" on this website: https://www.selenium.dev/documentation/en/getting_started/quick/

My Code is:

driver.get("https://www.selenium.dev/documentation/en/getting_started/quick/");
WebElement elem = driver.findElement(By.linkText("Edit this page"));
Actions actions = new Actions(driver);
		Thread.sleep(3000);
		actions.moveToElement(elem)
		.click()
		.keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL)
        .build().perform();

With this code it will be marked all texts on page. How can i mark only the Hypelink?

答案1

得分: 1

代替使用 Actions,尝试使用 JavascriptExecutor 来设置元素的背景颜色:

WebElement elem = driver.findElement(By.linkText("Edit this page"));
((JavascriptExecutor)driver).executeScript("arguments[0].style.backgroundColor='lightblue'", elem);

需要添加以下导入语句:

import org.openqa.selenium.JavascriptExecutor;

我尝试了以下操作,得到了如下结果:
如何使用Selenium Java标记超链接

英文:

Instead you use Actions, try using JavascriptExecutor by setting the background color of the element:

WebElement elem = driver.findElement(By.linkText("Edit this page"));
((JavascriptExecutor)driver).executeScript("arguments[0].style.backgroundColor='lightblue'", elem);

You need following import:

import org.openqa.selenium.JavascriptExecutor;

I've tried with following result:
如何使用Selenium Java标记超链接

huangapple
  • 本文由 发表于 2020年7月24日 05:44:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/63063557.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定