Select a text and perform a click action

后端 未结 3 1783
故里飘歌
故里飘歌 2021-02-04 15:35

I\'d like to select some text and perform a click action - like in Winword where we click Bold after selecting some text...

I have to select the text and cl

相关标签:
3条回答
  • 2021-02-04 15:57

    I tried this way and it did not work. Here are the codes:

    System.setProperty("webdriver.chrome.driver", "D:/chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.google.com.vn");
        driver.manage().window().maximize();
    
        WebElement text = driver.findElement(By.xpath("//*[contains(text(),'Google.com.vn')]"));
        Actions actions = new Actions(driver);
        actions.moveToElement(text, 10, 5).clickAndHold().moveByOffset(30, 0).release().perform();
    

    I switched to JavascriptExecutor and it worked:

        System.setProperty("webdriver.chrome.driver", "D:/chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.google.com.vn");
        driver.manage().window().maximize();
    
        WebElement text = driver.findElement(By.xpath("//*[contains(text(),'Google.com.vn')]"));
        JavascriptExecutor js = (JavascriptExecutor) driver;
    
        js.executeScript("arguments[0].setAttribute('style', 'background: blue;');", text);
    
    0 讨论(0)
  • 2021-02-04 16:08

    I tried with Action builder and played with offset. It worked for me.

    Actions action = new Actions(driver);
    action.moveToElement(wblmt,3,3).click().keyDown(Keys.SHIFT).moveToElement(wblmt,200, 0).click().keyUp(Keys.SHIFT).build().perform(); 
    
    0 讨论(0)
  • 2021-02-04 16:12

    In Java, The Advanced User Interactions API has your answer.

    // the element containing the text
    WebElement element = driver.findElement(By.id("text"));
    // assuming driver is a well behaving WebDriver
    Actions actions = new Actions(driver);
    // and some variation of this:
    actions.moveToElement(element, 10, 5)
        .clickAndHold()
        .moveByOffset(30, 0)
        .release()
        .perform();
    
    0 讨论(0)
提交回复
热议问题