我想找的是:
getElementByXpath(//html[1]/body[1]/div[1]).innerHTML
我需要得到元素的innerHTML使用JS(使用硒WebDriver/Java,因为WebDriver不能找到它本身),但如何?
我可以使用ID属性,但不是所有元素都有ID属性。
(固定)
我使用jsoup在Java中完成它。这符合我的需要。
我想找的是:
getElementByXpath(//html[1]/body[1]/div[1]).innerHTML
我需要得到元素的innerHTML使用JS(使用硒WebDriver/Java,因为WebDriver不能找到它本身),但如何?
我可以使用ID属性,但不是所有元素都有ID属性。
(固定)
我使用jsoup在Java中完成它。这符合我的需要。
当前回答
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");
}
}
确保您使用了正确的定位器。
其他回答
**Different way to Find Element:**
IEDriver.findElement(By.id("id"));
IEDriver.findElement(By.linkText("linkText"));
IEDriver.findElement(By.xpath("xpath"));
IEDriver.findElement(By.xpath(".//*[@id='id']"));
IEDriver.findElement(By.xpath("//button[contains(.,'button name')]"));
IEDriver.findElement(By.xpath("//a[contains(.,'text name')]"));
IEDriver.findElement(By.xpath("//label[contains(.,'label name')]"));
IEDriver.findElement(By.xpath("//*[contains(text(), 'your text')]");
Check Case Sensitive:
IEDriver.findElement(By.xpath("//*[contains(lower-case(text()),'your text')]");
For exact match:
IEDriver.findElement(By.xpath("//button[text()='your text']");
**Find NG-Element:**
Xpath == //td[contains(@ng-show,'childsegment.AddLocation')]
CssSelector == .sprite.icon-cancel
在Chrome开发工具中,您可以运行以下命令:
$x("some xpath")
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);
你可以使用document.evaluate:
方法的结果 如果可能,请指定类型。
它是w3标准化的,并且有完整的文档:https://developer.mozilla.org/en-US/docs/Web/API/Document.evaluate
函数getElementByXpath(path) { 返回文档。(path, document, null, XPathResult.).singleNodeValue FIRST_ORDERED_NODE_TYPE, null); } console.log(getElementByXpath("//html[1]/body[1]/div[1]")); < div > foo < / div >
https://gist.github.com/yckart/6351935
在mozilla开发者网络上也有一个很好的介绍:https://developer.mozilla.org/en-US/docs/Introduction_to_using_XPath_in_JavaScript#document.evaluate
替代版本,使用XPathEvaluator:
函数getElementByXPath(xpath) 返回新的XPathEvaluator() .createExpression (xpath) XPathResult.FIRST_ORDERED_NODE_TYPE .evaluate(文档) .singleNodeValue } console.log(getElementByXPath("//html[1]/body[1]/div[1]")); < div > foo / bar < / div >
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");
}
}
确保您使用了正确的定位器。