Reputation: 391
I developed a selenium script, which makes automatic comments in facebook groups.
It works relatively good, but it does not execute the click()
method if the targeted element is not visible on the browser.
As a working around I'm using the execute_script("window.scrollTo(x,y";)
, method, but it's not ideal script. The piece of code that must be improved is the following:
text_box = driver.find_element_by_class_name("UFIAddCommentInput")
try:
driver.execute_script("window.scrollTo(100, 0);")
text_box.click()
except:
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
text_box.click()
element = driver.switch_to.active_element
element.send_keys(frase)
element.send_keys(Keys.RETURN)
It first tries for the element on the top of the page, and, if does not get to execute click()
, it tries at the bottom.
However, there is a more effective way to scroll at the element found by the find_element_by_class_name
method?
Upvotes: 2
Views: 969
Reputation: 52665
You can try
text_box.location_once_scrolled_into_view
text_box.click()
to scroll page down right to required element and click it
Upvotes: 3