javascript - Selenium - 如何获得被屏蔽的图像?

标签 javascript html css selenium selenium-webdriver

HTML:

<html>
<head>
<body onload="document.getElementById('a').style.display='block';">
<div id="a" align="center" onclick="document.location.reload();" style="display: block; cursor: pointer;">
<img width="91" height="24" alt="" src="/CFFileServlet/_cf_captcha/_captcha_img7840336618954261679.png">
</div>
</body>
</html>

Java:

WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@id='a']")));

我无法访问此图片或 div。我想获取图像源。创建主体时,它会被阻塞。

错误:

Exception in thread "main" org.openqa.selenium.TimeoutException: Timed out after 20 seconds waiting for visibility of element located by By.xpath: //div[@id='a']
Build info: version: '2.46.0', revision: '87c69e2', time: '2015-06-04 16:16:47'
System info: host: 'android-PC', ip: '192.168.1.103', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_79'
Session ID: cecb1517-719e-46b1-bbbf-fd44babd7e5b
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=WINDOWS, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, nativeEvents=false, webStorageEnabled=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=38.0.5}]
    at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:80)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:229)
    at com.hack.ImageDownload.main(ImageDownload.java:92)
Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//div[@id='a']"}
Command duration or timeout: 8 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.46.0', revision: '87c69e2', time: '2015-06-04 16:16:47'
System info: host: 'android-PC', ip: '192.168.1.103', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_79'
*** Element info: {Using=xpath, value=//div[@id='a']}
Session ID: cecb1517-719e-46b1-bbbf-fd44babd7e5b
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=WINDOWS, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, nativeEvents=false, webStorageEnabled=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=38.0.5}]
    at sun.reflect.GeneratedConstructorAccessor10.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:605)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:358)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:455)
    at org.openqa.selenium.By$ByXPath.findElement(By.java:358)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:350)
    at org.openqa.selenium.support.ui.ExpectedConditions.findElement(ExpectedConditions.java:809)
    at org.openqa.selenium.support.ui.ExpectedConditions.access$0(ExpectedConditions.java:807)
    at org.openqa.selenium.support.ui.ExpectedConditions$7.apply(ExpectedConditions.java:205)
    at org.openqa.selenium.support.ui.ExpectedConditions$7.apply(ExpectedConditions.java:1)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:209)
    ... 1 more
Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//div[@id='a']"}
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.46.0', revision: '87c69e2', time: '2015-06-04 16:16:47'
System info: host: 'android-PC', ip: '192.168.1.103', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_79'
Driver info: driver.version: unknown
    at <anonymous class>.FirefoxDriver.prototype.findElementInternal_(file:///C:/Users/android/AppData/Local/Temp/anonymous8077319824186785062webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:10299)
    at <anonymous class>.FirefoxDriver.prototype.findElement(file:///C:/Users/android/AppData/Local/Temp/anonymous8077319824186785062webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:10308)
    at <anonymous class>.DelayedCommand.prototype.executeInternal_/h(file:///C:/Users/android/AppData/Local/Temp/anonymous8077319824186785062webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12282)
    at <anonymous class>.DelayedCommand.prototype.executeInternal_(file:///C:/Users/android/AppData/Local/Temp/anonymous8077319824186785062webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12287)
    at <anonymous class>.DelayedCommand.prototype.execute/<(file:///C:/Users/android/AppData/Local/Temp/anonymous8077319824186785062webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12229)

最佳答案

请尝试 xpath = //div[@id='a']/img

关于javascript - Selenium - 如何获得被屏蔽的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31137091/

相关文章:

javascript - 如何将函数链与异步函数一起使用?

javascript - 如何使表格可按标题 Angular 排序?

html - 使用 twitter bootstrap 表时旋转表头

html - CSS |列表项上方单个垂直列中的有序列表

html - 音乐平面符号显示额外的填充并增加行高

Javascript:Html2canvas 回调

javascript - 如何存储重量值并转换为标准/公制重量?

javascript - 通过 jQuery 通过内部按钮更改 iFrame 的页面

javascript - 如何在不修改 HTML 内容的情况下加载和解析 HTML

html - CSS3打字效果在最后切掉一些文字