英文:
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;
英文:
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;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论