Select a text and perform a click action

后端 未结 3 1782
故里飘歌
故里飘歌 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);
    

提交回复
热议问题