1

Maybe you met similar problem in your testing live...

During execution of the test I recived information form console that element could not be scrolled into view. I found out that probably using java script executor is the solution. It's a dropdown.

There are 2 onclicks, which refer to 2 stages of action, all of them include dynamic number:

onclick="selector('phone_dynamicnumberlike98046454')" - general
onclick="pick('phone_dynamicnumberlike454674645',event)" - choose the type from dropdown

So if I want to use it I probably have to refer to dynamic id as an argument:

driver.execute_script(selector(dynamic id?);)

Now I refer to dynamic id using starts-with and contains:

driver.find_element_by_xpath("(//*[starts-with(@id, 'phone') and 
contains(@id, 'dropdown')])").click()

And in next line I refer by xpath and by value to the type of phone.

How do I correctly replace this line of code by using javascript executor? I mean what is best practise in python, selenium?

Anand
  • 1,899
  • 1
  • 13
  • 23
ann
  • 113
  • 2
  • 3
  • 9
  • Possible duplicate of [WebDriver click() vs JavaScript click()](https://stackoverflow.com/questions/34562061/webdriver-click-vs-javascript-click) – JeffC Feb 12 '18 at 17:10
  • The link above explains the difference between the two types of clicks. You might not care about the full explanation but you can use the code sample in the question. You don't need to use JS to *find* the element, you can locate it using Selenium and then pass the located element into the JS function. – JeffC Feb 12 '18 at 17:12
  • Can you update the question with your exact _Manual Steps_ and the relevant _HTML_ ? – undetected Selenium Feb 13 '18 at 08:04

2 Answers2

0

Why not use the scroll-into-view method instead of JS executor?

from selenium.webdriver.common.action_chains import ActionChains

element = driver.find_element_by_xpath("(//*[starts-with(@id, 'phone') and 
contains(@id, 'dropdown')])")

actions = ActionChains(driver)
actions.move_to_element(element).perform()
Anand
  • 1,899
  • 1
  • 13
  • 23
0

Have you tried scrolling to the element using getBoundingClientRect() and scrollTo()

element = driver.find_element_by_xpath("(//*[starts-with(@id, 'phone') and contains(@id, 'dropdown')])")
driver.execute_script("coordinates = arguments[0].getBoundingClientRect();scrollTo(coordinates.x,coordinates.y);", element)

Also is confusing what you are asking because when you have two onclick attributes on the same element they both get triggered with click().

Eduard Florinescu
  • 16,747
  • 28
  • 113
  • 179