2

I have quite a unique goal and I'm having a hard time to have my python code working. Inside a big selenium application, I'm trying simply to check if an element located on a specific position in the browser corresponds to an element. For example, if you look at the test website: https://learn.letskodeit.com/p/practice there's one element (link) labeled "Open Tab" and its coordinates on the browser are: x = 588, y = 576. This is the code I'm using to confirm in that position I have that element:

target_elem = driver.find_element_by_id("opentab")
print("target elem actual location: {}".format(target_elem.location))
time.sleep(1)

zero_elem = driver.find_element_by_tag_name('body')
x_body_offset = zero_elem.location["x"]
y_body_offset = zero_elem.location["y"]
print("Body coordinates: {}, {}".format(x_body_offset, y_body_offset))

x = 588
y = 576

actions = ActionChains(driver)
actions.move_to_element_with_offset(driver.find_element_by_tag_name('body'), -x_body_offset, -y_body_offset).click()
actions.move_by_offset( x, y ).send_keys(Keys.ESCAPE).perform()

elem_found = driver.switch_to.active_element
print(elem_found.text)

when I print elem_found.text I don't get "Open Tab". however, if inside the action chain, right before perform(), I add click(), the code above does click on the "Open Tab" link. Hence my question: can we simply select the element by knowing its exact position in the browser? I totally understand getting element by location is not really the best way to get element but on my end, I do really need to be able to confirm if an element in position X,Y corresponds to something I expect to find in that location.

Angelo
  • 1,594
  • 5
  • 17
  • 50
  • You can use sikuli with selenium .. to locate element in that position – Ankur Singh Aug 28 '18 at 03:10
  • @Ankur Singh many thanks for your comment, it's the first time I hear about sikuli. I spent some time searching information on the web but it seems like it's not really integrated with selenium since it works on jython. Could you point me to any good resources to see how to use sikuli with selenium? – Angelo Aug 28 '18 at 17:18
  • check this post you will get idea :- https://stackoverflow.com/questions/18198121/calling-to-a-sikuli-script-from-python-selenium – Ankur Singh Aug 28 '18 at 17:21
  • @AnkurSingh thank you Ankur. I have to admit that solution seems a little bit over my head, I was hoping to have a solution sticking to selenium but thank you very much for your help. – Angelo Aug 28 '18 at 20:26

0 Answers0