MusterTester
MusterTester

Reputation: 79

How to mark a Hyperlink with Selenium Java

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?

Upvotes: 0

Views: 61

Answers (1)

frianH
frianH

Reputation: 7563

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: Result

Upvotes: 1

Related Questions