【问题标题】:How to mark a Hyperlink with Selenium Java如何使用 Selenium Java 标记超链接
【发布时间】:2020-11-13 17:29:09
【问题描述】:

我正在尝试在此网站上标记超链接“编辑此页面”: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();

使用此代码,它将标记页面上的所有文本。我怎样才能只标记超链接?

【问题讨论】:

    标签: java selenium hyperlink href


    【解决方案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;
    

    我尝试了以下结果:

    【讨论】:

    • 您好 frianH,感谢您的回答。您已设置元素的背景颜色。我不知道,当你用鼠标标记链接文本时是否是同样的事情???这与以下内容是否相同:keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL)
    • @MusterTester 那不一样。但我认为,如果您的问题是“如何标记超链接”,这是一种简单的实现方式。问题是你接下来想通过标记这些元素来实现什么,所以你必须使用Actions 方法?
    猜你喜欢
    • 2012-08-15
    • 1970-01-01
    • 2020-11-02
    • 1970-01-01
    • 2020-07-14
    • 1970-01-01
    • 2013-10-04
    • 2020-04-22
    • 2017-02-02
    相关资源
    最近更新 更多