【问题标题】:Python selenium webdriver: list index out of range maximum 5 elementsPython selenium webdriver:列表索引超出范围最多5个元素
【发布时间】:2021-06-17 09:38:39
【问题描述】:

你好,我在 twitter 上使用 selenium 来点击多个回复按钮,driver.find_elements_by_xpath 我面临的问题是我只能点击 5 个元素而不是更多。这是列表的打印版

[<selenium.webdriver.remote.webelement.WebElement (session="cc78e4de72bea5cfc9499af03f3e9271", 
element="c9beaf38-e9a9-46af-baa8-eff476c87be8")>, <selenium.webdriver.remote.webelement.WebElement 
(session="cc78e4de72bea5cfc9499af03f3e9271", element="7cd7649f-efc5-48b5-9e68-3d97a96e72cc")>, 
<selenium.webdriver.remote.webelement.WebElement (session="cc78e4de72bea5cfc9499af03f3e9271", 
element="6d1e9ce9-9b17-4f69-8c49-0805331549fd")>, <selenium.webdriver.remote.webelement.WebElement 
(session="cc78e4de72bea5cfc9499af03f3e9271", element="1c4f540d-2dfe-4ed5-80ba-01bf5ba1a389")>, 
<selenium.webdriver.remote.webelement.WebElement (session="cc78e4de72bea5cfc9499af03f3e9271", 
element="0eaa0f99-0e62-45cf-bc57-fef86ac30e03")>]

你可以清楚地数出 5 个元素

这是我的代码

reply1 = driver.find_elements_by_xpath('//div[@data-testid="reply"]/div/div[1]')
count = 0
reply1[count].click()
                
                
count = count + 1
print (count)
print(reply1)
sleep(4)
driver.back()


except common.exceptions.ElementClickInterceptedException:
   time.sleep(2)                                                                          
   driver.execute_script("window.scrollTo(0, window.scrollY + 200)")                             
   time.sleep(2)
                

我收到此错误:

reply1[count].click()
IndexError: list index out of range
PS C:\Users\92\Desktop\new features test\new feature> 
[7300:18956:0320/013757.708:ERROR:ssl_client_socket_impl.cc(924)] handshake failed; returned -1, SSL 
error code 1, net_error -101

更新:我做了一些更改,代码正在处理 9 个元素,

count = 0 

running = True
while running:
 
            

    loop = True
    while loop:

    reply1 = driver.find_elements_by_xpath('//div[@data-testid="reply"]/div/div[1]')
    
    reply = reply1[count] 

    sleep(1)
    
    
    print(reply)
    reply1[count].click()

    #comment_buttons[count].click()

    
    sleep(4)


    driver.back()
    loop = False
    

count = count + 1
print (count)

           
sleep(1) 
time.sleep(2)  
                                                                     
driver.execute_script("window.scrollTo(0, window.scrollY + 200)")
                                               
print ("d2")                        
time.sleep(2)

打开 9 个元素后,我收到此错误:

Traceback (most recent call last):
File "c:\Users\92\Desktop\new features test\new feature\test copy.py", line 
155, in <module>
reply = reply1[count]
IndexError: list index out of range

【问题讨论】:

  • 如果它只找到 5 则要么尝试向下滚动(如果它有延迟加载)或等待所有元素都存在。
  • 我确实滚动并等待超过 30 秒它不起作用,而是跳过 5 个元素并在接下来的 5 个元素上工作

标签: python selenium error-handling webdriver


【解决方案1】:

我尝试了 2 种类型,在下面的第一个代码中,ElementClickInterceptedException 在 7 cmets 按钮打开后出现,我尝试通过滚动来处理它,但它一直在滚动,最大的问题是在这两种情况下推文显然没有在后台加载

for _ in range(6):         # loop as many times as you want to scroll
tweets = driver.find_elements_by_xpath('//div[@data-testid="reply"]/div/div[1]')
for tweet in tweets:

    tweet.click()
    sleep(2)
    driver.back()
driver.execute_script("window.scrollTo(0, window.scrollY + 200)")
sleep(3)

我尝试的第二个代码是在 11 cmets 按钮上工作,然后 IndexError: list index out of range 来了

 count = 0 
 for _ in range(20):
     try:

 
          
    tweets = driver.find_elements_by_xpath('//div[@data-testid="tweet"]/div/div[1]')
    print(tweets)
    for tweet in tweets:
        reply = driver.find_elements_by_xpath('//div[@data-testid="reply"]/div/div[1]')
        reply[count].click()
        #print(reply)
        sleep(2)
        driver.back()
        
        count = count + 1
        print(count)
    driver.execute_script("window.scrollTo(0, window.scrollY + 200)")
    sleep(3)



  except common.exceptions.ElementClickInterceptedException:
        
        time.sleep(2)                                                                          
        driver.execute_script("window.scrollTo(0, window.scrollY + 200)")
        
        print ("except ! ")                              
        time.sleep(2)  

【讨论】:

  • 我确信代码不起作用还有更重要的原因,但首先,您不需要计数器,因为您使用的是 for 循环。另外,数组“tweets”和“replies”有什么区别?我觉得一个就够了。除此之外,滚动是否有效?因为如果回复按钮不在您的视口中,它通常无法单击它。
【解决方案2】:

正如@Arundeep Chohan 在他的评论中提到的,Twitter 有一个称为“延迟加载”的功能,你必须手动向下滚动才能看到下一条推文。

在您的代码中,您似乎正在尝试滚动,使用

driver.execute_script("window.scrollTo(0, window.scrollY + 200)") 

但是这段代码放在“except”子句中。我不确定它是否会被执行,因为该错误 (ElementClickInterceptedException) 可能不会发生。你只有5个元素,都可以点击,不拦截点击。

但是,您之前增加了计数器,但不知道回复 1 是否包含第 6 条推文(似乎没有)。

所以,我建议您将“滚动”代码放在异常之外。使用循环在开始时滚动您想要的次数,将每次找到的元素存储在列表中,最后,迭代该列表并单击每个元素中的回复按钮。

更新:我认为你的新代码比以前更令人困惑,但你已经接近了。我将尝试用一些伪代码来解释它:

for _ in range(3):         # loop as many times as you want to scroll
    tweets = find elements 
    for tweet in tweets:
        click_reply_button
        sleep
    scroll_window
    sleep

【讨论】:

  • 如何在列表中存储元素?
  • 我已经做了一些事情,但现在很奇怪,它在前 5 个元素上运行良好,然后跳过接下来的 5 个元素,然后在接下来的 5 个元素上工作,检查我的更新
  • 我认为问题在于您执行每个步骤的顺序:首先,您收集 5 个元素,然后遍历它们中的每一个,单击每个元素的按钮,同时您尝试滚动。可能会更好:(1)像你一样收集5个元素,(2)循环遍历每个元素并单击它,(3)退出循环,滚动窗口。然后,使用外部循环,重复整个过程,从 1 到 3。每次滚动时,您可能会有一些重复,因为之前的推文会被再次收集。
  • 我应该如何遍历它们并单击它,我做了一些更正我已经编辑了上面的代码,现在它正在处理 9 个元素,这很奇怪
  • 我试过了,但是ElementClickInterceptedException来了,我也尝试通过滚动来处理它,但是滚动不停for _ in range(6): scroll tweets = driver.find_elements_by_xpath('//div[@data-testid="reply"]/div/div[1]') for tweet in tweets: tweet.click() sleep(2) driver.back() driver.execute_script("window.scrollTo(0, window.scrollY + 200)") sleep(3)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多