Is there a way to get element by XPath using JavaScript in Selenium WebDriver?

后端 未结 10 1625
后悔当初
后悔当初 2020-11-22 10:53

I am looking for something like:

getElementByXpath(//html[1]/body[1]/div[1]).innerHTML

I need to get the innerHTML of elements using JS (to

10条回答
  •  北荒
    北荒 (楼主)
    2020-11-22 11:50

    To direct to the point , you can easily use xapth .The exact and simple way to do this using the below code . Kindly try and provide feedback .Thank you .

    JavascriptExecutor js = (JavascriptExecutor) driver;
    
        //To click an element 
        WebElement element=driver.findElement(By.xpath(Xpath));
        js.executeScript(("arguments[0].click();", element);
    
        //To gettext
    
        String theTextIWant = (String) js.executeScript("return arguments[0].value;",driver.findElement(By.xpath("//input[@id='display-name']")));
    

    Further readings - https://medium.com/@smeesheady/webdriver-javascriptexecutor-interact-with-elements-and-open-and-handle-multiple-tabs-and-get-url-dcfda49bfa0f

提交回复
热议问题