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

后端 未结 10 1464
后悔当初
后悔当初 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:36

    For something like $x from chrome command line api (to select multiple elements) try:

    var xpath = function(xpathToExecute){
      var result = [];
      var nodesSnapshot = document.evaluate(xpathToExecute, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );
      for ( var i=0 ; i < nodesSnapshot.snapshotLength; i++ ){
        result.push( nodesSnapshot.snapshotItem(i) );
      }
      return result;
    }
    

    This MDN overview helped: https://developer.mozilla.org/en-US/docs/Introduction_to_using_XPath_in_JavaScript

    0 讨论(0)
  • 2020-11-22 11:41

    In Chrome Dev Tools you can run the following:

    $x("some xpath")
    
    0 讨论(0)
  • 2020-11-22 11:43

    Assuming your objective is to develop and test your xpath queries for screen maps. Then either use Chrome's developer tools. This allows you to run the xpath query to show the matches. Or in Firefox >9 you can do the same thing with the Web Developer Tools console. In earlier version use x-path-finder or Firebug.

    0 讨论(0)
  • 2020-11-22 11:43
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    
    System.setProperty("webdriver.chrome.driver", "path of your chrome exe");
            WebDriver driver = new ChromeDriver();
            driver.manage().window().maximize();
            driver.get("https://www.google.com");
    
                driver.findElement(By.xpath(".//*[@id='UserName']")).clear();
                driver.findElement(By.xpath(".//*[@id='UserName']")).sendKeys(Email);
    
    0 讨论(0)
  • 2020-11-22 11:49

    You can use javascript's document.evaluate to run an XPath expression on the DOM. I think it's supported in one way or another in browsers back to IE 6.

    MDN: https://developer.mozilla.org/en-US/docs/Web/API/Document/evaluate

    IE supports selectNodes instead.

    MSDN: https://msdn.microsoft.com/en-us/library/ms754523(v=vs.85).aspx

    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题