首页 文章

aria-hidden span / button阻止获取元素 - Selenium

提问于
浏览
1

我试图使用Selenium with 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">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"));

}

(要么)

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'并点击按钮?

3 回答

  • 0

    为了达到所需的控制,您可以使用其容器 . 因此,请尝试以下方法

    //div[@data-date-picker-id='book-trip']//button[starts-with(@id,'dp') and starts-with(@aria-label, 'Departing on ')]
    

    让我知道,它是否适合你 .

  • 0

    试试这个按钮的xpath -

    //div[@class = 'dates-picker-wrapper splited-date-picker flight-datepicker']/div[1]/button
    

    我在Firefox中检查了这个,它对我有用 .

  • 0

    尝试使用此JavaScript代码以启用所选元素的可见性 .

    WebElement element = driver.findElement(By.xpath("Element Xpath"));
    String js = "arguments[0].style.height='auto'; arguments[0].style.visibility='visible';";
    ((JavascriptExecutor) driver).executeScript(js, elem);
    

相关问题