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

后端 未结 10 1543
后悔当初
后悔当初 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条回答
  •  -上瘾入骨i
    2020-11-22 11:53

    public class JSElementLocator {
    
        @Test
        public void locateElement() throws InterruptedException{
            WebDriver driver = WebDriverProducerFactory.getWebDriver("firefox");
    
            driver.get("https://www.google.co.in/");
    
    
            WebElement searchbox = null;
    
            Thread.sleep(1000);
            searchbox = (WebElement) (((JavascriptExecutor) driver).executeScript("return document.getElementById('lst-ib');", searchbox));
            searchbox.sendKeys("hello");
        }
    }
    

    Make sure you are using the right locator for it.

提交回复
热议问题