Selenium Firefox WebDrive,使用Python在div中滚动 [英] Selenium Firefox webdrive, using Python, scrolling in div
问题描述
我已经使用python一段时间了,我想保存需要事先登录的特定网页.由于该网站使用javascript,因此我决定将硒用于Firefox和Firefox WebDrive.我能够登录.但是该网站要求我先接受EULA,然后才能访问所需的页面.
I have been using python for a while, I want to save specific webpages which require prior login. Since this website uses javascript, I decided to use selenium for python with firefox webdrive. I was able to login. But the site requires me to accept EULA before I can access the required pages.
问题是我必须向下滚动整个文本(使用单独的div滚动条),然后才能单击接受.我对Selenium和javascript完全陌生.
The problem is that I have to scroll down the entire text (with a seperate div scrollbar) before I can click accept. I am completely new to selenium and javascript.
提供的用户名和密码有效,一旦找到解决方案,我将对其进行更改.
The provided username and password are valid and I will change them once I get a solution.
dr = webdriver.Firefox()
dr.get("https://freida.ama-assn.org/Freida/user/programDetails.do?pgmNumber=1401611114")
dr.find_element_by_id("go_username").clear()
dr.find_element_by_id("go_username").send_keys("hajayd")
dr.find_element_by_id("go_password").clear()
dr.find_element_by_id("go_password").send_keys("123456")
dr.find_element_by_id("Image1").click()
dr.get("https://freida.ama-assn.org/Freida/eula.do")
# code to scroll down eula here
dr.find_element_by_id("agreeBtn").click()
dr.get("https://freida.ama-assn.org/Freida/user/programDetails.do?pgmNumber=1401611114")
# Final page I want to visit
有可能吗?还有其他可接受的解决方案吗?
Is it possible? Is there any other acceptable solution?
推荐答案
You can use WebDriver.execute_script
to execute javascript:
eula = dr.find_element_by_id('eulaFrame')
dr.execute_script('arguments[0].scrollTop = arguments[0].scrollHeight', eula)
可以使用arguments[..]
访问传递给javascript的
自变量(在上例中为eula
).
Arguments (eula
in above example) passed to javascript can be accessed using arguments[..]
.
侧面说明:如果在javascript代码中使用return
,则该值可用作execute_script
的返回值.
Side note: If you use return
in the javascript code, that value is available as the return value of the execute_script
.
这篇关于Selenium Firefox WebDrive,使用Python在div中滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!