java – PhantomJs swithToFrame不起作用

    <dependency>
        <groupId>com.github.detro</groupId>
        <artifactId>phantomjsdriver</artifactId>
        <version>1.2.0</version>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.0.1</version>
    </dependency>

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-support</artifactId>
        <version>3.0.1</version>
    </dependency>

当我使用phantomjsdriver下面的代码时,我无法在切换到Frame后通过css选择器 – table.b3id-widget-table.b3-widget-table找到元素.该元素位于iframe内.

同时我能够使用具有相同代码的Firefox或Chrome二进制文件找到此元素.似乎在切换到帧后,phantomjs无法在iframe中找到任何元素.任何线索?

wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("standalone-container-main-widgetIframe")));
driver.switchTo().frame("standalone-container-main-widgetIframe");
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("table.b3id-widget-table.b3-widget-table")));

如果iframe位于另一个原点,则另一个答案建议添加以下选项.

String [] phantomJsArgs = {"--ignore-ssl-errors=true", "--ssl-protocol=any", "--web-security=false"};
dCaps.setCapability(PhantomJSDriverService.PHANTOMJS_GHOSTDRIVER_CLI_ARGS, phantomJsArgs);
dCaps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, phantomJsArgs);

但它对我没有帮助.最奇怪的事情,有时它工作几次,从50次尝试以某种方式1次.我改变了不同的超时,隐含的等待,明确的等待 – 没有任何帮助,甚至等待3-4分钟.似乎不是时间问题.另外我注意到我的iframe没有src属性,也许这就是问题所在

 <iframe frameborder="0" src="about:blank" id="standalone-container-main-widgetIframe">

此外,我使用PhantomJs截取屏幕截图,看起来页面已成功加载,包括嵌套的iframe,截图由于某种原因被裁掉.

最佳答案 你有没有尝试过 …

driver.FindElement(By.Name("standalone-container-main-widgetIframe"));
string frameHTML = driver.SwitchTo().Frame("standalone-container-main-widgetIframe").PageSource;
doc.LoadHtml(frameHTML);

所以这样你也可以看到你的帧html.您还需要使用htmlagilitypack …

点赞