使用(Python) Webdriver在不使用元素的情况下选择文本(例如,单击和拖动从一组坐标到另一组的高亮显示)

时间:2020-12-13 21:44:46

I am trying to select some text (i.e. highlight it with the mouse cursor) for an automated test. I would like to use Python and webdriver to go to this url: http://en.wikipedia.org/wiki/WebDriver#Selenium_WebDriver and highlight the second sentence under the heading 'Selenium WebDriver' ("Selenium WebDriver accepts commands (sent in Selenese, or via a Client API) and sends them to a browser.")

我正在尝试选择一些文本(也就是用鼠标光标突出显示)来进行自动化测试。我想使用Python和webdriver来访问这个url: http://en.wikipedia.org/wiki/WebDriver#Selenium_WebDriver,并在标题“Selenium webdriver”下高亮第二个句子(“Selenium webdriver接受命令(通过Selenese发送,或者通过客户机API发送)并将它们发送到浏览器”)。

The tricky thing is that I was hoping that this could be done without using any elements, and I've been trying to work out a way to click at a location specified by x and y coordinates and then hold moving to another location, specified by a different set of x and y coordinates.

棘手的是,我希望这样做可以不使用任何元素,我一直试图找出一种点击指定的位置由x和y坐标,然后移动到另一个位置,指定的一组不同的x和y坐标。

From reading around, I understand that it is not possible to just click on an area of the page by coordinates as you need to specify an element, so can the text selection be done only using only a single, remote element (let's say ".mw-editsection>a")? I was thinking it might be possible be able to do it by using the element as a reference and clicking a certain distance away from it (i.e. click by offset).

从阅读中,我了解到,不可能只在需要指定一个元素的情况下,在页面的某个区域上单击鼠标,那么文本选择只能使用一个单独的远程元素(比方说“.mw-editsection>a”)?我想可能可以通过使用元素作为参考,并点击一定的距离(即点击偏移)。

This is what I've attempted so far, but it's not doing the job:

这是我迄今为止尝试过的,但它并没有起到作用:

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Firefox()
actions = ActionChains(driver)
driver.get("http://en.wikipedia.org/wiki/WebDriver#Selenium_WebDriver")

the_only_element = ".mw-editsection>a"
element = driver.find_element_by_css_selector(the_only_element)

actions.move_to_element_with_offset(element,50,50)
actions.click_and_hold(on_element=None)
actions.move_by_offset(50, 50)
actions.release()
actions.perform()

From this, I get this error:

从这里,我得到了这个错误:

WebDriverException: Message: u"'UnknownError: Cannot press more then one button or an already pressed button.' when calling method: [wdIMouse::down]" 

Background:

I appreciate that the above example is a bit contrived, but I can't actually provide you with the thing I'm really trying to test. What I'm actually doing is writing a series of tests in Python using webdriver to test our document viewer, and I really need to be able to highlight a row of text as this is how you add comments on our system. Unfortunately, the document viewer doesn't actually show the submitted document, only an image of it via some kind of javascript wizardry. The document page is an element, but there are no elements for webdriver to click on within the page itself.

我很感激上面的例子有点做作,但是我不能提供给你我真正想要测试的东西。实际上,我所做的是在Python中使用webdriver编写一系列的测试来测试我们的文档查看器,我真的需要能够高亮显示一行文本,因为这是您在我们的系统上添加注释的方式。不幸的是,文档查看器实际上并没有显示所提交的文档,只有通过某种javascript向导的图像。文档页面是一个元素,但是没有元素可以让webdriver在页面本身中单击。

Because of this, I want to be able to click and hold on a location on the page by specifying coordinates (at the start of the sentence), keep the mouse button held while the mouse curser is simulated moving to the right to a second set of coordinates (at the end of the sentence), and then release the button.

正因为如此,我希望能点击并按住一个位置在页面上通过指定坐标(在句子的开始),保持鼠标按钮在鼠标遥控器举行模拟向右移动第二组坐标(在句子的结尾),然后释放按钮。

TL;DR:

Is it possible to click and drag from an arbitrary point on a webpage to another, without using an element (other than to act as a reference from which the arbitrary point is defined as being offset from)?

是否可以在不使用元素的情况下从网页上的任意点上点击并拖拽到另一个点(除了作为一个引用,任意点被定义为被偏移)?

If not, what other method could you suggest to highlight an area of text and could you provide a working example?

如果没有,您还可以建议什么其他方法来突出显示一个文本区域,您可以提供一个工作示例吗?

Thanks!

谢谢!

2 个解决方案

#1


0  

Have you tried with drag_and_drop_by_offset ?

您尝试过drag_and_drop_by_offset吗?

actions.drag_and_drop_by_offset(element, 50, 50)
actions.perform()

#2


0  

I think I've worked it out- The following does seem to work, but I can't seem to get it to let go! I think this might be down to a buggy implementation of .release() in the Python bindings for Webdriver:

我想我已经算出来了——下面的方法似乎有效,但我似乎无法让它放手!我认为这可能是由于Webdriver的Python绑定中的.release()的bug实现。

def click_and_drag(locator, x_from, y_from, x_to, y_to):
    element = driver.find_element_by_css_selector(locator)
    actions.move_to_element(element)
    actions.move_by_offset(x_from, y_from)
    actions.click_and_hold(on_element=None)
    actions.move_by_offset(x_to, y_to)
    actions.release(on_element=None)
    actions.perform()

#1


0  

Have you tried with drag_and_drop_by_offset ?

您尝试过drag_and_drop_by_offset吗?

actions.drag_and_drop_by_offset(element, 50, 50)
actions.perform()

#2


0  

I think I've worked it out- The following does seem to work, but I can't seem to get it to let go! I think this might be down to a buggy implementation of .release() in the Python bindings for Webdriver:

我想我已经算出来了——下面的方法似乎有效,但我似乎无法让它放手!我认为这可能是由于Webdriver的Python绑定中的.release()的bug实现。

def click_and_drag(locator, x_from, y_from, x_to, y_to):
    element = driver.find_element_by_css_selector(locator)
    actions.move_to_element(element)
    actions.move_by_offset(x_from, y_from)
    actions.click_and_hold(on_element=None)
    actions.move_by_offset(x_to, y_to)
    actions.release(on_element=None)
    actions.perform()