首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Selenium web驱动程序:选择器无效:*:-webkit-full-screen-ancestor

Selenium web驱动程序:选择器无效:*:-webkit-full-screen-ancestor
EN

Stack Overflow用户
提问于 2015-08-02 01:25:28
回答 1查看 530关注 0票数 0

所以下面的代码以前是可以工作的,但是现在我运行它,它总是给我这个异常。该代码主要用于在youtube的搜索栏中输入内容,然后单击搜索按钮。

代码:

代码语言:javascript
复制
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为:

代码语言:javascript
复制
< input id="masthead-search-term" autocomplete="off" autofocus="" onkeydown="if (!this.value &amp;&amp; (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为:

代码语言:javascript
复制
<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,但给了我同样的异常。

异常跟踪:

代码语言:javascript
复制
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。如果有任何答案,我都会很高兴。

EN

回答 1

Stack Overflow用户

发布于 2015-08-02 13:48:21

无法使用Keys.ENTER。这是更正后的代码-:

代码语言:javascript
复制
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"));
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31764275

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档