gpt4 book ai didi

java - 无法使用 Selenium 和 Java 单击 Web 元素

转载 作者:行者123 更新时间:2023-11-30 05:23:53 25 4
gpt4 key购买 nike

我尝试了 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 元素已成功创建。但是,当我尝试使用多种交互方法与其交互时,什么也没有发生。

enter image description here

我得到的异常:org.openqa.selenium.ElementNotInteractableException:元素不可交互

最佳答案

我会尝试查询文本,而不仅仅是 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,以查询“Got it”文本,而不是可能返回多个结果的 ID 属性 content。我还包含了执行 Javascript 点击的代码,这可能有助于解决任何点击问题。

Xfinity 网站似乎有许多 iframe 元素,但我无法进入您的特定页面进行检查和查看,因此这可能会也可能不会导致问题。

关于java - 无法使用 Selenium 和 Java 单击 Web 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59035143/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com