我试图在一个网页上使用以下代码单击几个元素,但它给出了元素不可交互的错误。我还应用了等待条件,但没有帮助。
这是我的代码:
driver.get("https://www.cleartrip.com/flights/results?from=BDQ&to=PNQ&depart_date=13/04/2019&adults=1&childs=0&infants=0&class=Economy&airline=&carrier=&intl=n&sd=1555000238907&stops=1&departureTime=0_8");
driver.findElement(By.xpath("//input[@value='1' and @name = 'stops']")).click();
driver.findElement(By.xpath("//input[@value='2' and @name = 'stops']")).click();
driver.findElement(By.xpath("//input[@value='0_8' and @name = 'departureTime']")).click();
错误:
FAILED: testFlightSearch
org.openqa.selenium.ElementNotVisibleException: element not interactable
(Session info: chrome=73.0.3683.86)
(Driver info: chromedriver=73.0.3683.20 (8e2b610813e167eee3619ac4ce6e42e3ec622017),platform=Windows NT 10.0.17134 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
Build info: version: '3.141.0', revision: '2ecb7d9a', time: '2018-10-31T20:09:30'
System info: host: 'DESKTOP-B0K7HHH', ip: '192.168.43.195', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_161'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 73.0.3683.20 (8e2b610813e16..., userDataDir: C:\Users\shakti\AppData\Loc...}, cssSelectorsEnabled: true, databaseEnabled: false, goog:chromeOptions: {debuggerAddress: localhost:62726}, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, proxy: Proxy(), rotatable: false, setWindowRect: true, strictFileInteractability: false, takesHeapSnapshot: true, takesScreenshot: true, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unexpectedAlertBehaviour: ignore, unhandledPromptBehavior: ignore, version: 73.0.3683.86, webStorageEnabled: true}
Session ID: 8931562f8f37533e51073920887d83b0
答案 0 :(得分:1)
您尝试单击的元素显示在页面上,但不可单击/不可交互,这就是您遇到element not interactable
异常的原因。
您需要使用下面提到的xpath来选择元素,这将起作用:
driver.get("https://www.cleartrip.com/flights/results?from=BDQ&to=PNQ&depart_date=13/04/2019&adults=1&childs=0&infants=0&class=Economy&airline=&carrier=&intl=n&sd=1555000238907&stops=1&departureTime=0_8");
driver.findElement(By.xpath("//label[@for='1_1_1']")).click();
driver.findElement(By.xpath("//label[@for='1_1_2']")).click();
driver.findElement(By.xpath("//label[@for='1_1_0_8_departureTime']")).click();
答案 1 :(得分:1)
获取org.openqa.selenium.ElementNotVisibleException
的原因是因为当硒进入该URL时,页面顶部有一个进度条,该进度条仍在加载页面内容。在加载时,您的代码正在搜索要单击的第一个元素,但尚未将其加载到DOM中。
在触发元素点击之前,您需要等待进度条完成加载。加载时的进度条具有xpath://p[@class='loadState tCenter' and contains(text(), 'Getting prices and availability...')]
,完全加载后,要查找的xpath是//p[@class='loadState tCenter' and contains(text(), 'Your search results are ready.')]
。
添加一个等待元素,使其出现在上面的第二个xpath中,一旦它可见,便触发元素单击。