硒如何单击屏幕上50%的元素和不屏幕上50%的元素? [英] How does Selenium click on elements that are 50% on screen and 50% not on screen?

查看:229
本文介绍了硒如何单击屏幕上50%的元素和不屏幕上50%的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个div元素.屏幕大小的50%在屏幕上.其余50%超出屏幕高度,因此不可见. 无法滚动.

There is a div-Element. 50% of its size are on the screen. The other 50% go over the screen height and are not visible. There is no scrolling possible.

我尝试使用Selenium自动化测试,然后单击该div元素,但有时它可以工作,有时却不能.

I tried to automate a test with Selenium and click on that div-element, but sometimes it works and sometimes it does not.

Selenium为什么不仅仅单击该div的屏幕区域"?

Why does Selenium not just click on the "on-screen-area" of that div?

该功能如何实现?当我告诉Selenium点击一个巨大的div元素时,它是否点击了该div上的随机位置?

And how is this functionality implemented? When I tell Selenium to click on a huge div-element, does it click on a random position on that div?

推荐答案

元素的视图内中心点

根据 WebDriver W3C规范,元素的视图中中心点是矩形的原点位置,它是元素的第一个视口.

element’s in-view center point

As per the WebDriver W3C Specification an element’s in-view center point is the origin position of the rectangle that is the intersection between the element’s first DOM client rectangle and the initial viewport.

考虑到已知的元素,可以通过以下方式进行计算:

Given an element that is known to be in view, it can be calculated this way:

  1. 让矩形成为通过调用元素上的getClientRects返回的DOMRect序列的第一个元素.
  2. 让左为max(0,min(x坐标,x坐标+宽度尺寸)).
  3. 让我们成为min(innerWidth,max(x坐标,x坐标+宽度尺寸)).
  4. 让top成为max(0,min(y坐标,y坐标+高度尺寸)).
  5. 让底部为min(innerHeight,max(y坐标,y坐标+高度尺寸)).
  6. 让x成为下限((左+右)÷2.0).
  7. 让我们成为地板((顶部+底部)÷2.0).
  8. 返回(x,y)对.

如果某个元素是其自己的可与指针交互的绘制树的成员,那么它就处于可见状态,前提是它的指针事件未禁用.

An element is in view if it is a member of its own pointer-interactable paint tree, given the pretense that its pointer events are not disabled.

根据文档,元素点击" 命令滚动到查看元素(如果还不能与指针交互),则单击其视图内中心点.

As per the documentation the Element Click command scrolls into view the element if it is not already pointer-interactable, and clicks its in-view center point.

注意:如果元素的中心点被另一个元素遮挡,则会返回元素点击拦截错误.如果元素在视口之外,则返回元素不可交互的错误.

Note: If the element’s center point is obscured by another element, an element click intercepted error is returned. If the element is outside the viewport, an element not interactable error is returned.

在这种情况下,有两种可能的解决方案,如下所示:

In such cases there are two possible solutionas follows:

  1. 您可以诱导

  1. You can induce WebDriverWait setting the expected_conditions as element_to_be_clickable(). So effectively your line of code will be:

WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".reply-button"))).click()

  • 您可以使用

  • You can use move_to_element(to_element) and click(on_element=None) method respectively. So effectively your line of code will be:

    ActionChains(driver).move_to_element(element).click(element).perform()
    


  • 参考

    您可以在 selenium.common中找到相关的讨论. exceptions.ElementClickInterceptedException:消息:拦截元素单击:Selenium和Python无法点击元素


    Reference

    You can find a relevant discussion in selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element is not clickable with Selenium and Python

    这篇关于硒如何单击屏幕上50%的元素和不屏幕上50%的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆