Reputation: 18735
I have a dropdown which html is list
- <ul><li><a></a></li>...
This dropdown is scrollable and I need to make Selenium
to scroll to <a>
with certain id
because I can't click on it unless it's visible.
It is probably possible using JavaScript
but can't figure out how to make it work.
clickable_a_id - id of <a> element which I want to scroll to
self.driver.execute_script(
# 'arguments[0].scrollTop = document.getElementById("{}").offsetTop + 5 '.format(clickable_a_id),
'document.getElementById("{}")[0].scrollIntoView()'.format(clickable_a_id),UL_ELEMENT)
It does not work. Do you know how to do that?
Upvotes: 0
Views: 1426
Reputation: 2115
To scroll to a specific element using Id, you can implement JavascriptExecutor
like below:
//Use some delay like wait.until or sleep
WebElement elem = driver.findElement(By.id("clickable_a_id"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", elem);
//Use some delay like wait.until or sleep
PS. You might(or not) need to use some sort of delay for visibility of Id element.
First, try without using delay.
Upvotes: 0
Reputation: 353
did you try using action ?
el = driver.find_element_by_xpath("")
webdriver.ActionChains(driver).move_to_element(el).perform()
you can scroll further with
driver.execute_script("window.scrollTo(0,20)")
Upvotes: 2