本文介绍了咏叹调隐藏的跨度/按钮阻止获取元素 - Selenium的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用 Selenium 和 Java(3.3.0 和 Java 版本1.8.0_66")从以下网站单击日历.
https://www.cathaypacific.com/cx/en_US.html
要点击的目标 - 航班 - 一种方式 - 出发"按钮不管我尝试了哪些可能的选项——by.id、by.xpath 和 Actions、EventFiringMouse 等,这个按钮根本没有被点击.
"<div class="button-date-picker-wrapper field-group cx-inputfield"><span class="field-label input-filled" aria-hidden="true">出发</span><button id="dppju1sm" class="button-date-picker field-button from-button has-dates input-filled" role="link" type="button" data-ui-overlay-shared="true"data-ui-overlay-id="trip-dates-picker" aria-expanded="false" aria-label="2017 年 4 月 20 日星期四出发">
"
private static void pickFlightCode() 抛出 InterruptedException {WebElement element = driver.findElement(By.xpath("//div[1]/button[starts-with(@id,'dp')]"));//wdwait.until(ExpectedConditions.elementToBeClickable(element));动作动作=新动作(驱动程序);actions.moveToElement(element).moveToElement(driver.findElement(By.xpath("//div[1]/button[starts-with(@id,'dp')]"))).click().build().履行();element = driver.findElement(By.xpath("//div[1]/button[starts-with(@id,'dp')]"));System.out.println(element.getAttribute("aria-hidden"));
}
(或)
driver.findElement(By.xpath("//div[1]/button[starts-with(@id,'dp')]")).click();String js = "document.getElementById("field-label").style.display = "block";';arguments[0].style.visibility='visible';";
上述代码不起作用,我收到元素不可见"异常.Driver.findElement - isEnabled 返回 true,Driver.findElement - isDisplayed 返回 false.
这与 span 中的 'aria-hidden'=true 属性有关吗?我们应该如何处理 'aria-hidden' 并点击按钮?
解决方案
为了达到所需的控件,您可以利用其容器.因此,请尝试以下操作:
//div[@data-date-picker-id='book-trip']//button[starts-with(@id,'dp') and starts-with(@aria-label,'出发')]
让我知道它是否适合您.
I am trying to click a calendar from the following website using Selenium with Java (3.3.0 and java version "1.8.0_66").
https://www.cathaypacific.com/cx/en_US.html
Target to be clicked - Flights - One Way - 'Departing On' buttonNo matter, what possible options I tried - by.id, by.xpath and Actions, EventFiringMouse etc., this button does not get clicked at all.
"<div class="button-date-picker-wrapper field-group cx-inputfield">
<span class="field-label input-filled" aria-hidden="true">Departing on</span>
<button id="dppju1sm" class="button-date-picker field-button from-button has-dates input-filled" role="link" type="button" data-ui-overlay-shared="true" data-ui-overlay-id="trip-dates-picker" aria-expanded="false" aria-label="Departing on Thursday 20 April 2017">
</div>"
private static void pickFlightCode() throws InterruptedException {
WebElement element = driver.findElement(By.xpath("//div[1]/button[starts-with(@id,'dp')]"));
//wdwait.until(ExpectedConditions.elementToBeClickable(element));
Actions actions=new Actions(driver);
actions.moveToElement(element).moveToElement(driver.findElement(By.xpath("//div[1]/button[starts-with(@id,'dp')]"))).click().build().perform();
element = driver.findElement(By.xpath("//div[1]/button[starts-with(@id,'dp')]"));
System.out.println(element.getAttribute("aria-hidden"));
}
(or)
driver.findElement(By.xpath("//div[1]/button[starts-with(@id,'dp')]")).click();String js = "document.getElementById("field-label").style.display = "block";';arguments[0].style.visibility='visible';";
The above code does not work and I am getting 'Element Not Visible' Exception.Driver.findElement - isEnabled returns true and Driver.findElement - isDisplayed returns false.
Is this something to do with the 'aria-hidden'=true attribute in span? How should we handle 'aria-hidden' and click on the button?
解决方案
In order to reach to the required control, you can make use of its container. Hence, try following:
//div[@data-date-picker-id='book-trip']//button[starts-with(@id,'dp') and starts-with(@aria-label, 'Departing on ')]
Let me know, whether it works for you.
这篇关于咏叹调隐藏的跨度/按钮阻止获取元素 - Selenium的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!