英文:
Inserting time delays between send_keys requests in Selenium webdriver
问题
我的初始问题是,当我获取我想要抓取的网页页面的page_source
时,我无法访问全部代码,因为只有当我滚动浏览网站上的不同框时,完整的代码才会加载。
我决定使用Selenium中的send_keys
方法多次按下"TAB"键,以浏览网页上的所有元素并加载完整的代码。
但是在使用以下代码时,"TAB"键按得太快,所以代码无法加载:
browser.find_element(by=By.ID, value='multi-selector-checkbox-ember44').send_keys(Keys.TAB * 500)
我尝试使用time.sleep
函数在每个send_keys(Keys.TAB)
重复之间添加时间延迟,但它不起作用。
英文:
My initial problem is that when I get the page_source
of a website page that I want to scrape, I don't have access to all the code because the full code only loads if I scroll through the different boxes on the site.
I decided to use the send_keys
method in Selenium to press the "TAB" key multiple times to go through all the elements on the website page and to load the full code.
But when using the following, the "TAB" key is pressed to rapidly, so the code does not load:
browser.find_element(by=By.ID,value='multi-selector-checkbox-ember44').send_keys(Keys.TAB * 500)
I tried using a time.sleep function to add time delays between each send_keys(Keys.TAB)
repetition, but it does not work.
答案1
得分: 0
我找到了一个解决方案:在页面上,每个级别都有类名为 'small-input' 的按钮,所以我使用了以下函数来向下滚动页面:
def scroll_down(self):
to_explore = browser.find_elements(by=By.CLASS_NAME, value='small-input')
for i in range(len(to_explore)):
to_explore[i].send_keys((Keys.TAB)*2)
time.sleep(.5)
英文:
Actually, I found a solution: on the page website, there are buttons of class 'small-input' at every level, so I used this function to scroll down the page:
def scroll_down(self):
to_explore = browser.find_elements(by=By.CLASS_NAME,value='small-input')
for i in range(len(to_explore)):
to_explore[i].send_keys((Keys.TAB)*2)
time.sleep(.5)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论