java - 使用 xpath 单击隐藏按钮时出现陈旧元素引用 : element is not attached to the page,

标签 java selenium selenium-webdriver xpath

我正在尝试使用 selenium 中的 xpath 单击以下按钮,

<div class="bg-iconButton-button bg-common-nodrag bg-tool-maximize bg-common-hideDisplay" title="Display in full screen" id="bg-po-full-screen-1"></div>

我尝试了下面的代码,

fullscreenbutton=UtilityFunc.driver.findElement(By.xpath("//*[@id='bg-po-full-screen-1']"));
KeywordFunc.fnClick(fullscreenbutton);

我收到以下错误,

org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document

我认为我面临这个问题,因为它是作为 div 而不是按钮封闭的。

最佳答案

首先,您可以通过元素的 ID 而不是 Xpath 来选择元素。

WebElement fullscreenbutton = UtilityFunc.driver.findElement(By.id("bg-po-full-screen-1"));

其次,您尝试单击的元素没有与之关联的 onClick 事件,也不是按钮。您确定您的实际按钮没有嵌套在其中或位于其下方吗?

关于java - 使用 xpath 单击隐藏按钮时出现陈旧元素引用 : element is not attached to the page,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61822438/

相关文章:

python - Selenium:查找相邻元素

java - Android - 输入流无法正常工作

c# - Selenium 截图参数无效

java - FirefoxDriver Selenium 不起作用 (Java)

javascript - Selenium Python 获取 &lt;script&gt; 标签信息?

python - 使用 Chrome 时出现 Selenium "selenium.common.exceptions.NoSuchElementException"

java - Android 错误 : DateFormat() sometimes shows 12hr format, 当它应该显示 24 小时制时

java - 从 JSOUP 解析的 HTML 中删除转义文本

java - 我如何解释这个 JVM 错误?

c# - 如何使用 Selenium WebDriver 滚动到元素