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.")
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.
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).
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]"
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.
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.
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 ?
actions.drag_and_drop_by_offset(element, 50, 50)
actions.perform()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With