使用硒突出显示文本 [英] Highlight text using Selenium

查看:82
本文介绍了使用硒突出显示文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个上下文相关菜单,需要突出显示文本才能使其正常工作.但是,我在使用Selenium选择文本时遇到问题.在尝试使用可用的不同鼠标事件与之交互之前,我先找到要查找的WebElement.

I have a context sensitive menu that needs text to be hightlighted in order for it to work. However, I'm having problems with selecting the text using Selenium. I start by finding the WebElement I'm looking for, before trying to interact with it using the different mouse events available.

当我尝试选择文本的某些部分时,除了将标记放在字符串的末尾外,它似乎没有做任何其他事情. 这是我的文本框的样子:

When I'm trying to select parts of the text, it doesn't appear to do anything other than placing the marker at the end of the string. This is what my textbox looks like:

这是我需要的外观,换句话说,我需要Selenium进行选择(出于说明目的,只是手动进行了此操作:

This is what I need it to look like, or in other words, what I need Selenium to select (Just did it manually for the purpose of illustration:

这与我要在代码中尝试做的事情类似:

This is along the lines of what I'm trying to do in code:

public static async Task HighlightElementByCssSelector(this RemoteWebDriver @this, string cssSelector, TimeSpan? timeout = null, TimeSpan? interval = null)
{
    var element = await @this.FindElementByCssSelectorAsync(".testmarker-registryentryedit .testmarker-title-field");
    Actions action = new Actions(@this).MoveToElement(element).ClickAndHold(element).MoveByOffset(10,0).Release();
    action.Build().Perform();
}

在这种情况下,

@this代表驱动程序,而FindElementByCssSelectorAsync是包装框架"的一部分. 我尝试了MoveToElement,DragAndDrop,ClickAndHold等的不同组合,但是我无法使其正常工作.关于这里可能出什么问题的任何指示?

@this represents the Driver in this case, and the FindElementByCssSelectorAsync being part of a 'wrapper-framework'. I've tried different combinations of MoveToElement, DragAndDrop, ClickAndHold etc, but I just can't get it to work. Any pointers as to what might be wrong here?

推荐答案

根据我对您的问题的了解,我尝试在本地计算机上解决此问题(休假的第一天,哈哈).抱歉,我在那台机器上没有VS,所以我用Java编写了它.该代码应该是不言自明的:

According to what I understood about your problem I tried to solve it on my local machine (first day of vacation, lol). Sorry, I don't have VS on that machine so I wrote it in Java. The code should be self-explanatory:

@org.junit.Test
public void doTest(){
    String query = "This is a test";
    WebDriver driver = new FirefoxDriver();
    driver.get("https://www.google.com");
    WebElement searchBox = new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.id("lst-ib")));
    searchBox.sendKeys(query);

    // make sure it has focus
    searchBox.click();

    Actions actions = new Actions(driver);
    // go to the beginning of input
    actions.sendKeys(Keys.HOME).build().perform();
    int length = query.substring(0, query.indexOf("a")).length();

    actions.keyDown(Keys.LEFT_SHIFT);
    for (int i = 0; i < length; i++){
        actions.sendKeys(Keys.ARROW_RIGHT);
    }
    actions.keyUp(Keys.LEFT_SHIFT);
    actions.build().perform();
}

结果:

这是您想要的吗?

这篇关于使用硒突出显示文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆