org.openqa.selenium.ElementNotVisibleException:元素不可见(会话信息:chrome = 55.0.2883.87)

时间:2017-01-06 07:04:38

标签: java google-chrome selenium accordion

请帮助我的Selenium代码。我只是一个初学者,无法弄清楚我的代码有什么问题。错误讯息:

Accordion down arrow clicked.
Time found.
FAILED: homeLoan("Janet Jones", "janet.jones@testing.com", "Australia", "4569", "62365898563", "Home Loan", "Buying", "150000", "55000")
org.openqa.selenium.ElementNotVisibleException: element not visible
  (Session info: chrome=55.0.2883.87)
  (Driver info: chromedriver=2.25.426923 (0390b88869384d6eb0d5d09729679f934aab9eed),platform=Windows NT 10.0.14393 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 25 milliseconds
Build info: version: '2.53.1', revision: 'a36b8b1cd5757287168e54b817830adce9b0158d', time: '2016-06-30 19:26:09'
System info: host: 'DEV-TEST-01', ip: '172.30.60.20', os.name: 'Windows NT (unknown)', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_112'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.25.426923 (0390b88869384d6eb0d5d09729679f934aab9eed), userDataDir=C:\Users\adm-cxc\AppData\Local\Temp\2\scoped_dir3468_7348}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=55.0.2883.87, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: eba36d4b2bcab09b464da0fbf99e681d

我的代码可以点击手风琴的向下箭头以及带有复选框的选择时间。但是,我的代码无法单击首选时间的复选框。

System.out.println("Switched to pop up window.");

driver.findElement(By.xpath("//*[@id='modalBookAppointment']/div/div/div/h4"));
System.out.println("When would you like us to call you? found.");

driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
driver.findElement(By.xpath("(//*[@class='fa fa-chevron-down'])[3]")).click();
System.out.println("Accordion down arrow clicked.");

driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);

driver.findElement(By.xpath("//*[@id='p_lt_ctl00_Firstmac_RetailHardSellBookAppointment_BookAppointment_AppointmentDatesRepeater_ctl01_BookAppointment_AppointmentsRepeater_ctl12_AppointmentSelection_Vacant']/div[1]/input"));
System.out.println("Time found.");

driver.findElement(By.xpath("//*[@id='p_lt_ctl00_Firstmac_RetailHardSellBookAppointment_BookAppointment_AppointmentDatesRepeater_ctl01_BookAppointment_AppointmentsRepeater_ctl12_AppointmentSelection_Vacant']/div[1]/input")).click();;
System.out.println("Time selected.");

我正在尝试选择早上9点的时间,第一个从屏幕截图中突出显示。

enter image description here

任何帮助都可以。提前谢谢。

3 个答案:

答案 0 :(得分:0)

我认为问题是你的xpath不正确。你的指向input标签,而thr immage中的部分指向{I}之前的div元素。

在您发布的图片中,“firePath”标签顶部显示了完整的正确路径。尝试使用那个,让我们知道它是否成功。

编辑: 您可以尝试以下操作来查看它是否是基于时间的问题。

WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.elementToBeClickable(yourXPath));

答案 1 :(得分:0)

尝试等待一段时间,直到目标元素变为可见:

JavascriptExecutor js = (JavascriptExecutor) driver;  
js.executeScript('document.querySelector("input[type='checkbox']").style.display="block";');

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@type='checkbox']")));

答案 2 :(得分:0)

我已经通过将xpath更改为// *来解决此问题:[@ id =' p_lt_ctl00_Firstmac_RetailHardSellBookAppointment_BookAppointment_AppointmentDatesRepeater_ctl01_BookAppointment_AppointmentsRepeater_ctl12_AppointmentSelection_Vacant']