简体   繁体   English

使用(Python)Webdriver选择文本而不使用元素(即单击并拖动以从一组坐标突出显示另一组坐标)

[英]Using (Python) Webdriver to select text without using an element (i.e. click and drag to highlight from one set of coordinates to another set)

I am trying to select some text (ie 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")? 通过阅读,我知道不可能只需要通过坐标点击页面区域,因为您需要指定一个元素,因此文本选择只能使用一个远程元素(让我们说“”)。 MW-Editsection的>一“)? 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 (ie click by offset). 我认为可以通过使用元素作为参考并点击距离它一定距离(即点击偏移)来做到这一点。

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. 我实际上在做的是使用webdriver在Python中编写一系列测试来测试我们的文档查看器,我真的需要能够突出显示一行文本,因为这是你在我们系统上添加注释的方式。 Unfortunately, the document viewer doesn't actually show the submitted document, only an image of it via some kind of javascript wizardry. 不幸的是,文档查看器实际上并不显示提交的文档,只是通过某种javascript魔法显示它的图像。 The document page is an element, but there are no elements for webdriver to click on within the page itself. 文档页面是一个元素,但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: 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! 谢谢!

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()

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()错误实现:

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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 `A.__new__(B)` 的用例,即 python 中的“使用一个 class 制作另一个” - Use cases for `A.__new__(B)` i.e. “using one class to make another” in python 是否可以通过鼠标双击选择文本并通过鼠标将选择文本拖动到 Selenium Webdriver Python 中的另一个 webElement - Is there a possibility to select text by mouse double click and drag the selectet text by mouse to another webElement in Selenium Webdriver Python 不使用任何现成库的Python中的神经网络。 - Neural Networks in Python without using any readymade libraries…i.e., from first principles..help! 使用 Python 和 selenium webdriver 单击没有 ID/名称的按钮元素 - Click on button element without id/name using Python and selenium webdriver 如何使用python selenium webdriver为html中的元素设置xpath - How to set the xpath for a element in html using python selenium webdriver 使用 Python 点击,如何添加超过 5 个选项,即超过 5 个? - Using Python Click, how do I add more than 5 options i.e. more than 5? 为什么不能使用python和Chrome Webdriver单击Selenium中的一个元素? - Why Cant I Click an Element in Selenium using python and Chrome Webdriver? 使用 Selenium 在窗口中单击并拖动以突出显示网页上的文本 - Using Selenium to Click and Drag Across the Window to Highlight Text on the Web Page 不使用Selenium WebDriver Python登出就无法从一个URL移到另一个URL - Can't move from one url to another without being logged out using selenium webdriver python 无法在python中使用webdriver单击元素 - Can't click on an element using webdriver in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM