在Selenium webdriver中在send_keys请求之间插入时间延迟

huangapple go评论133阅读模式
英文:

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)

huangapple
  • 本文由 发表于 2023年2月17日 23:37:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75486332.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定