所以下面的代码曾经工作但现在我运行了它总是给我这个例外。该代码主要用于在youtube的搜索栏中输入,然后单击搜索按钮。
代码:
WebDriver driver = new HtmlUnitDriver();
driver.get("https://www.youtube.com");
driver.findElement(By.name("search_query")).sendKeys("Selenium WebDriver"+Keys.ENTER);
driver.findElement(By.id("search-btn")).click();
List<WebElement> links = driver.findElements(By.tagName("a"));
for (int i = 0; i < links.size(); i++) {
// print the link text
System.out.println(links.get(i).getAttribute("title"));
// Print the href's
System.out.println(links.get(i).getAttribute("href"));
}
栏的html是:
< input id="masthead-search-term" autocomplete="off" autofocus="" onkeydown="if (!this.value && (event.keyCode == 40 || event.keyCode == 32 || event.keyCode == 34)) {this.onkeydown = null; this.blur();}" class="search-term masthead-search-renderer-input yt-uix-form-input-bidi" name="search_query" value="" type="text" tabindex="1" placeholder="" title="Search" dir="ltr" spellcheck="false" style="outline: none;">
搜索按钮的html为:
<button class="yt-uix-button yt-uix-button-size-default yt-uix-button-default search-btn-component search-button" type="submit" onclick="if (document.getElementById('masthead-search-term').value == '') return false; document.getElementById('masthead-search').submit(); return false;;return true;" id="search-btn" tabindex="2" dir="ltr"><span class="yt-uix-button-content">Search</span></button>
我尝试使用javascript打开和关闭,但给了我同样的例外。我尝试使用xpath,但给了我同样的例外。
异常追踪:
Exception in thread "main" org.w3c.css.sac.CSSException: Invalid selector: *:-webkit-full-screen-ancestor
at com.gargoylesoftware.htmlunit.javascript.host.css.CSSStyleSheet.validateSelectors(CSSStyleSheet.java:1195)
at com.gargoylesoftware.htmlunit.javascript.host.css.CSSStyleSheet.selectsPseudoClass(CSSStyleSheet.java:810)
at com.gargoylesoftware.htmlunit.javascript.host.css.CSSStyleSheet.selects(CSSStyleSheet.java:573)
at com.gargoylesoftware.htmlunit.javascript.host.css.CSSStyleSheet.selects(CSSStyleSheet.java:519)
at com.gargoylesoftware.htmlunit.javascript.host.css.CSSStyleSheet.selects(CSSStyleSheet.java:437)
at com.gargoylesoftware.htmlunit.javascript.host.css.CSSStyleSheet.selects(CSSStyleSheet.java:419)
at com.gargoylesoftware.htmlunit.javascript.host.css.CSSStyleSheet.modifyIfNecessary(CSSStyleSheet.java:251)
at com.gargoylesoftware.htmlunit.javascript.host.css.CSSStyleSheet.modifyIfNecessary(CSSStyleSheet.java:230)
at com.gargoylesoftware.htmlunit.javascript.host.Window.getComputedStyle(Window.java:1689)
at com.gargoylesoftware.htmlunit.html.DomNode.isDisplayed(DomNode.java:714)
at com.gargoylesoftware.htmlunit.html.HtmlElement.isDisplayed(HtmlElement.java:1671)
at org.openqa.selenium.htmlunit.HtmlUnitWebElement.isDisplayed(HtmlUnitWebElement.java:480)
at org.openqa.selenium.htmlunit.HtmlUnitWebElement$1.call(HtmlUnitWebElement.java:276)
at org.openqa.selenium.htmlunit.HtmlUnitWebElement$1.call(HtmlUnitWebElement.java:1)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.implicitlyWaitFor(HtmlUnitDriver.java:1353)
at org.openqa.selenium.htmlunit.HtmlUnitWebElement.verifyCanInteractWithElement(HtmlUnitWebElement.java:273)
at org.openqa.selenium.htmlunit.HtmlUnitWebElement.sendKeys(HtmlUnitWebElement.java:325)
at selenium.Counter2.main(Counter2.java:39)
第39行是提供的示例中的第3行。 对不起,如果有任何遗漏或误导。这是第一次使用stackoverflow。我很高兴得到任何答案。
答案 0 :(得分:0)
无法使用Keys.ENTER。这是更正后的代码 - :
WebDriver driver = new HtmlUnitDriver();
driver.findElement(By.name("search_query")).sendKeys("Selenium WebDriver");
driver.findElement(By.id("search-btn")).click();
List<WebElement> links = driver.findElements(By.tagName("a"));
for (int i = 0; i < links.size(); i++) {
// print the link text
System.out.println(links.get(i).getAttribute("title"));
// Print the href's
System.out.println(links.get(i).getAttribute("href"));
}