我正在尝试解析此链接的html" http://dev.sencha.com/extjs/5.0.0/examples/desktop/index.html"使用jsoup
,但它给我的全部是
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Desktop</title>
<!-- The line below must be kept intact for Sencha Cmd to build your application -->
<script type="text/javascript">
<!-- here it shows some script -->
</script>
</head>
<body>
</body>
</html>
如何提取记事本图标的属性,以便我可以使用webdriver点击它?
答案 0 :(得分:1)
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Main {
public static void main(String[] args) throws Exception {
WebDriver driver = new FirefoxDriver();
driver.get("http://dev.sencha.com/extjs/5.0.0/examples/desktop/index.html");
WebElement notepadIcon = driver.findElement(By.id("Notepad-shortcut"));
notepadIcon.click();
WebElement notepadDiv = driver.findElement(By.id("notepad"));
driver.switchTo().frame(notepadDiv.findElement(By.id("notepad-editor-inputCmp-iframeEl")));
//Now you have the notepad DOM
System.out.println("Page title is: " + driver.getPageSource());
driver.quit();
}
}
旁注:
我尝试使用HtmlUnitDriver
,但您可以看到here似乎存在问题。
引用用户 nlotz
HtmlUnit使用的JavaScript引擎在:first-childselector上使用(在&gt; = 2.2.1中使用)。
解决方法: Ext.override(Ext.Button,{buttonSelector:'button:first'}); (将恢复 到&lt; = 2.2)
中使用的选择器
<强>更新强>
ChromeDriver
的问题是它太快了。在我打开调试器之前,我有相同的异常(NoSuchElement
)。然后一切正常,因为断点给了网络应用程序时间
完成执行所需的javascript代码。经过一番研究,这就是我想出来的
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Main {
public static void main(String[] args) throws Exception {
System.setProperty("webdriver.chrome.driver", "PATH_TO_CHROMEDRIVER.EXE");
ChromeOptions options = new ChromeOptions();
options.addArguments("test-type");
WebDriver driver = new ChromeDriver(options);
driver.get("http://dev.sencha.com/extjs/5.0.0/examples/desktop/index.html");
//This sets an expected condition for the icon. It has to be clickable in 5 seconds.
//After that an Exception will be raised
WebElement notepadIcon = (new WebDriverWait(driver, 5))
.until(ExpectedConditions.elementToBeClickable(By.id("Notepad-shortcut")));
notepadIcon.click();
WebElement notepadDiv = driver.findElement(By.id("notepad"));
driver.switchTo().frame(notepadDiv.findElement(By.id("notepad-editor-inputCmp-iframeEl")));
//Now you have the notepad DOM
System.out.println("Page source: " + driver.getPageSource());
driver.quit();
}
}