Czy istnieje sposób na uzyskanie elementu przez XPath przy użyciu JavaScript w Selenium WebDriver?

Szukam czegoś takiego:

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

Muszę zdobyć innerHTML elementów za pomocą JS (aby użyć tego w Selenium WebDriver / Java, ponieważ WebDriver sam nie może go znaleźć), ale jak?

Mógłbym użyć atrybutu ID, ale nie wszystkie elementy mają atrybut ID.

[FIXED]

Używam jsoup, aby zrobić to w Javie. To pasuje do moich potrzeb. Dzięki za odpowiedzi. :)

Author: Ben, 2012-05-15

6 answers

Możesz użyć document.evaluate:

Oblicza łańcuch wyrażeń XPath i zwraca wynik określony typ, jeśli to możliwe.

Jest W3-standaryzowany i cały documentend: https://developer.mozilla.org/en-US/docs/Web/API/Document.evaluate

function getElementByXpath(path) {
  return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
}

console.log( getElementByXpath("//html[1]/body[1]/div[1]") );
<div>foo</div>

Https://gist.github.com/yckart/6351935

Jest też świetne wprowadzenie do mozilla developer network: https://developer.mozilla.org/en-US/docs/Introduction_to_using_XPath_in_JavaScript#document.evaluate
 279
Author: yckart,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-04-12 00:39:28

W Chrome Dev Tools możesz uruchomić:

$x("some xpath")
 108
Author: Dmitry Semenyuk,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-07-15 18:12:59

Dla czegoś takiego jak $x z interfejsu Chrome command line api (aby wybrać wiele elementów) spróbuj:

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;
}

Ten przegląd MDN pomógł: https://developer.mozilla.org/en-US/docs/Introduction_to_using_XPath_in_JavaScript

 13
Author: Jay,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-06-20 16:55:27

Możesz użyć dokumentu javascript.ewaluuje , aby uruchomić wyrażenie XPath w DOM. Myślę, że jest to obsługiwane w taki czy inny sposób w przeglądarkach z powrotem do IE 6.

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

IE obsługuje selectNodes zamiast tego.

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

 9
Author: RobG,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-04-16 02:43:17
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");
    }
}

Upewnij się, że używasz odpowiedniego lokalizatora.

 3
Author: Prerit Jain,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-08-22 08:29:00

Zakładając, że twoim celem jest opracowanie i przetestowanie zapytań xpath dla map ekranowych. Następnie użyj narzędzi programistycznych Chrome . Pozwala to uruchomić zapytanie xpath, aby wyświetlić dopasowania. Lub w Firefoksie > 9 możesz zrobić to samo z Web Developer Tools console . We wcześniejszej wersji użyj x-path-finder lub Firebug.

 2
Author: Martin Spamer,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-02-22 15:28:32