gpt4 book ai didi

java - 由于页脚,Selenium 元素点击被拦截

转载 作者:行者123 更新时间:2023-12-01 17:44:01 26 4
gpt4 key购买 nike

我写了这段代码:

try{
Actions actions = new Actions(driver);
actions.moveToElement(element);
actions.perform();
robot.delay(500);
element.click();
}catch(WebDriverException clickIntercepted){
robot.keyPress(KeyEvent.VK_PAGE_DOWN);
robot.keyRelease(KeyEvent.VK_PAGE_DOWN);
element.click();
}

元素“element”位于视口(viewport)之外,但是当我移动到该元素时,页面滚动直到该元素位于视口(viewport)中,但它位于页脚后面,因此当我尝试单击它时,我会得到一个单击拦截的异常。为了绕过这个问题,我按下向下翻页键并释放,问题是只有当我的浏览器位于屏幕顶部时它才起作用,如果我正在做其他事情它就不起作用。是否可以在不使用 javascriptexecutor 或按向下翻页键的情况下滚动直到元素不被页脚覆盖?我也尝试过

moveToElement(element, x, y)

但它与 moveToElement(element) 相同

这是页面底部的 HTML 代码:

<div id="bottom">
<div id="errorboxcomponent" class="">
<div id="decisionTB" style="display:none;">
<div id="errorBoxDecision" class="errorBox TBfixedPosition">
<div class="head"><h4 id="errorBoxDecisionHead">-</h4></div>
<div class="middle">
<span id="errorBoxDecisionContent">-</span>
<div class="response">
<div style="float:left; width:180px;">
<a href="javascript:void(0);" class="yes"><span id="errorBoxDecisionYes">.</span></a>
</div>
<div style="float:left; width:180px;">
<a href="javascript:void(0);" class="no"><span id="errorBoxDecisionNo">.</span></a>
</div>
<br class="clearfloat">
</div>
</div>
<div class="foot"></div>
</div>
</div>

<div id="fadeBox" class="fadeBox fixedPostion" style="display:none;">
<div>
<span id="fadeBoxStyle" class="success"></span>
<p id="fadeBoxContent"></p>
</div>
</div>

<div id="notifyTB" style="display:none;">
<div id="errorBoxNotify" class="errorBox TBfixedPosition">
<div class="head"><h4 id="errorBoxNotifyHead">-</h4></div>
<div class="middle">
<span id="errorBoxNotifyContent">-</span>
<div class="response">
<div>
<a href="javascript:void(0);" class="ok">
<span id="errorBoxNotifyOk">.</span>
</a>
</div>
<br class="clearfloat">
</div>
</div>
<div class="foot"></div>
</div>
</div>
</div>
</div>
<script type="text/javascript">
initBuffBar();

initIndex();

</script>

<div id="siteFooter">
<div class="content" style="font-size:10px">
<div class="fleft textLeft">
<a class="tooltip js_hideTipOnMobile overlay" href="URL;component=changelog&amp;ajax=1" data-overlay-class="popupWidthFixed" data-overlay-iframe="true" data-iframe-width="680" data-overlay-title="Note di versione" title="Note di versione">7.2.1</a>
<a class="homeLink" href="URL" target="_blank">...</a>
</div>
<div class="fright textRight">
<a href="URL" target="_blank">Help</a>|
<a href="URL" target="_blank">Forum</a>|
<a class="overlay" href="URL;component=rules&amp;ajax=1" data-overlay-iframe="true" data-iframe-width="450" data-overlay-title="Regole">Rules</a>|
<a href="URL;product=..." target="_blank">Contatti</a>
</div>
</div>
</div>

这是元素:

<img src="https://g1.ge.gsv.net/cdn9d/8e0e60340445budse18a1804b42f179.gif" width="16" height="16" alt="moon" class="icon-moon">

页脚固定在 DOM 的底部,因此元素位于其后面。

在这里,您可以在 moveToElement(element) 之后看到页脚后面的“月亮”元素,如标记的元素

Immagine.png

最佳答案

尝试以下解决方案:

WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("Your element Id")));

Actions builder=new Actions(driver);
builder.moveToElement(element).click().build().perform();

关于java - 由于页脚,Selenium 元素点击被拦截,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60894974/

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