簡體   English   中英

無法在 Java 中使用 Selenium 單擊 Web 元素

[英]Unable to click on Web Element using Selenium with Java

我嘗試了 WebDriverWait、.click()、.sendKeys(Keys.RETURN)、implicitWait、explicitWait 和更多方法,但我無法單擊此 Web 元素。

 <div class="actions style-scope tv-overlay-record-on-the-go"> <tv-button pill-action="" class="style-scope tv-overlay-record-on-the-go x-scope tv-button-2 left"><button class="style-scope tv-button"><div class="wrapper style-scope tv-button"><iron-icon id="icon" class="style-scope tv-button x-scope iron-icon-0"></iron-icon><div id="content" role="presentation" class="style-scope tv-button">Got it</div></div></button> <div class="hover-hint style-scope tv-button"> </div> </tv-button> </div>

基於上述 HTML 代碼,我創建了以下 xpath:

WebElement gotIt = driver.findElement(By.xpath("//div[@class='actions style-scope tv-overlay-account-active']//div[@id='content']"));

gotIt.click();

我相信代碼運行並確認按鈕在那里,因此成功創建了 web 元素。 但是,當我嘗試使用許多交互方法與它交互時,沒有任何反應。

在此處輸入圖片說明

我得到的異常: org.openqa.selenium.ElementNotInteractableException: element not interactable

我會嘗試查詢文本,而不僅僅是div ID 屬性——您的 XPath 可能會返回許多結果,然后單擊第一個結果,這就是為什么什么也沒有發生的原因。

我會試試這個:

WebElement gotIt = driver.findElement(By.xpath("//div[@class='actions style-scope tv-overlay-account-active']//div[text()='Got it']"));

//gotIt.click(); throws element not interactable

// you can also try Javascript click -- work around element not interactable issue
((JavascriptExecutor)driver).executeScript("arguments[0].click();", gotIt);

我將您的 XPath 更改為查詢“知道了”文本,而不是可能返回多個結果的 ID 屬性content 我還包含了執行 Javascript 點擊的代碼,這可能有助於解決任何點擊問題。

Xfinity 網站似乎有很多iframe元素,但我無法進入您的特定頁面進行檢查和查看,因此這也可能會或可能不會導致問題。

檢查您的按鈕是否在 iframe 上,如果不是,則您可以在 XPath 下方找到使用 contains 單擊“知道了”按鈕

WebDriverWait wait1 = new WebDriverWait(driver, 10);
WebElement element1 = wait1.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[contains(text(),'Got it')]")));
element1.click();

解決方案2:

WebDriverWait wait1 = new WebDriverWait(driver, 10);
WebElement element1 = wait1.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[contains(text(),'Got it')]")));
Actions builder = new Actions(driver);
Action buttonClick = builder.moveToElement(element1).click().build();

請嘗試單擊操作對象,如下所示:

    WebElement gotIt = driver.findElement(By.xpath("//div[@class='actions style-scope tv-overlay-account-active']//div[@id='content']"));

    Actions actions = new Actions(driver);
    actions.moveToElement(gotIt).click().build().perform();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM