我想找的是:

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

我需要得到元素的innerHTML使用JS(使用硒WebDriver/Java,因为WebDriver不能找到它本身),但如何?

我可以使用ID属性,但不是所有元素都有ID属性。

(固定)

我使用jsoup在Java中完成它。这符合我的需要。


当前回答

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

其他回答

在Chrome开发工具中,您可以运行以下命令:

$x("some xpath")

假设您的目标是开发和测试用于屏幕映射的xpath查询。然后使用Chrome的开发工具。这允许您运行xpath查询来显示匹配。或者在Firefox >9中,你可以用Web开发人员工具控制台做同样的事情。在早期版本中使用x- pathfinder或Firebug。

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

确保您使用了正确的定位器。

要使用xpath和javascript识别WebElement,必须使用evaluate()方法,该方法计算xpath表达式并返回结果。


document.evaluate ()

document.evaluate()返回一个基于XPath表达式和其他给定参数的XPathResult。

语法为:

var xpathResult = document.evaluate(
  xpathExpression,
  contextNode,
  namespaceResolver,
  resultType,
  result
);

地点:

xpathExpression: The string representing the XPath to be evaluated. contextNode: Specifies the context node for the query. Common practice is to pass document as the context node. namespaceResolver: The function that will be passed any namespace prefixes and should return a string representing the namespace URI associated with that prefix. It will be used to resolve prefixes within the XPath itself, so that they can be matched with the document. null is common for HTML documents or when no namespace prefixes are used. resultType: An integer that corresponds to the type of result XPathResult to return using named constant properties, such as XPathResult.ANY_TYPE, of the XPathResult constructor, which correspond to integers from 0 to 9. result: An existing XPathResult to use for the results. null is the most common and will create a new XPathResult


示范

例如,谷歌主页中的搜索框可以使用xpath作为//*[@name='q']来唯一识别,也可以使用Google -chrome-devtools控制台通过以下命令来识别:

$x("//*[@name='q']")

快照:

同样的元素也可以用document.evaluate()和xpath表达式来标识,如下所示:

document.evaluate("//*[@name='q']", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;

快照:

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