我在用java+selenium+chromewebdriver爬数据的时候,
这个网站frame内的table表格内又嵌套了iframe,我不知道为什么我无法通过
switchto().frame(id)获取其他方法定位到此iframe。求大佬解惑
WebDriver webDriver = getWebDriver("dataUrls");
try {
TimeUnit.SECONDS.sleep(2);
WebDriver driver = webDriver.switchTo().frame("SI2_mem_index");
driver.findElement(By.id("username")).sendKeys("jv39737");
driver.findElement(By.id("passwords")).sendKeys("aaa222");
driver.findElement(By.xpath("//div[@class=\"za_button\"]")).click();
TimeUnit.SECONDS.sleep(3);
WebDriver index = driver.switchTo().frame("SI2_mem_index");
//第一种方式
WebDriver body = index.switchTo().frame("body");
//第二种方式
WebDriver frame = index.switchTo().frame(0);
//第三种方式
WebElement iframe = index.findElement(By.tagName("iframe"));
index.switchTo().frame(iframe);
} catch (Exception e) {
webDriver.quit();
e.printStackTrace();
}
org.openqa.selenium.JavascriptException: javascript error: Failed to execute 'getComputedStyle' on 'Window': parameter 1 is not of type 'Element'.
(Session info: chrome=75.0.3770.100)
Build info: version: '3.9.1', revision: '63f7b50', time: '2018-02-07T22:25:02.294Z'
System info: host: 'DESKTOP-DP6GTVA', ip: '192.168.18.2', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_211'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 75.0.3770.100, chrome: {chromedriverVersion: 75.0.3770.90 (a6dcaf7e3ec6f..., userDataDir: C:\Users\admin\AppData\Loca...}, goog:chromeOptions: {debuggerAddress: localhost:52747}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: 437da2bca20d8f3601e3eee6bb3f9205
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)