Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
320 views
in Technique[技术] by (71.8m points)

I can't manage to get to scroll down in Selenium using python

I am trying to scroll down the page because the help element overlaps the next page button after I click the button once because the pages moves down. The next button is above the match history table. I tried using

driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

nothing is happening, then I tried using

element=driver.find_element_by_xpath("//table[@class='Table flx-sm dir-r-sm jst-s-sm flx-nw-sm ialgn-st-sm']")
actions = ActionChains(driver)
actions.move_to_element(element).perform()

it gives me the error: MoveTargetOutOfBoundsException: Message: (1009, 835) is out of bounds of viewport width (1536) and height (750)

I tried using

html = driver.find_element_by_tag_name('html')
html.send_keys(Keys.END)

Nothing is moving still.This is the code and the website that I am trying it on.(I also tried using Chrome but I had the same result)

driver = webdriver.Firefox()
url2="https://app.senpai.gg/lol/profile/eune/Primm"
driver.get(url2)
button = driver.find_element_by_xpath('/html/body/div[2]/div/div/section/div/section/main/div[1]/div[1]/div[6]/div/div[1]/div[2]/div/button[2]')
    time.sleep(10)
while true:
    if(button.is_enabled() and button.is_displayed()):
        button.click();
    else:
        break;
question from:https://stackoverflow.com/questions/66055322/i-cant-manage-to-get-to-scroll-down-in-selenium-using-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
$('[class="el-main"]').scrollBy(0,$('[class="el-main"]').scrollHeight)    

you have to find the scroll element and then use scrolBY on that

driver.execute_script("$('[class="el-main"]').scrollBy(0,$('[class="el-main"]').scrollHeight);")

if you get $ not defined use:

driver.execute_script("arguments[0].scrollBy(0,arguments[0].scrollHeight);", driver.find_element_by_class_name('el-main'))

You can also do:

driver.get(url2)

driver.find_element_by_class_name('el-main').click()

driver.find_element_by_css_selector("body").send_keys(Keys.END)

FIrst you have to bring scroll to focus by clicking it and then send END key on body

finding locator:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...