【问题标题】:Clicking button with Selenium not working单击 Selenium 的按钮不起作用
【发布时间】:2022-01-19 16:16:22
【问题描述】:

https://fbref.com/en/squads/0cdc4311/Augsburg-Stats 提供了将表格转换为 csv 的按钮,我想将其抓取。我点击像

这样的按钮
elements = driver.find_elements(By.XPATH, '//button[text()="Get table as CSV (for Excel)"]')
for element  in elements:
    element.click()

但我得到一个例外

ElementNotInteractableException:消息:元素不可交互

这是我要点击的元素。

这是完整的代码(我添加了 Adblock plus 作为 Chrome 扩展,应该配置为本地测试):

import pandas as pd
import bs4
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.chrome.options import Options
import time
import os

#activate adblock plus
path_to_extension = '/home/andreas/.config/google-chrome/Default/Extensions/cfhdojbkjhnklbpkdaibdccddilifddb/3.11.4_0'
options = Options()
options.add_argument('load-extension=' + path_to_extension)

#uses Chrome driver in usr/bin/ from https://chromedriver.chromium.org/downloads
driver = webdriver.Chrome(options=options)

#wait and switching back to tab with desired source
time.sleep(5) 
driver.switch_to.window(driver.window_handles[0])

NO_OF_PREV_SEASONS = 5

df = pd.DataFrame()

urls = ['https://fbref.com/en/squads/247c4b67/Arminia-Stats']

for url in urls:
    driver.get(url)
    html = driver.page_source
    soup = bs4.BeautifulSoup(html, 'html.parser')

    #click button -> accept cookies
    element = driver.find_element(By.XPATH, '//button[text()="AGREE"]')
    element.click()
    
    for i in range(NO_OF_PREV_SEASONS):
        elements = driver.find_elements(By.XPATH, '//button[text()="Get table as CSV (for Excel)"]')
        for element  in elements:
            element.click()
        
        #todo: get data
        
        #click button -> navigate to next page
        time.sleep(5)
        element = driver.find_element(By.LINK_TEXT, "Previous Season")
        element.click()
    
driver.quit()

【问题讨论】:

    标签: python selenium web-scraping


    【解决方案1】:

    button 在下拉列表中(即<span>Share & Export</span>),因此您需要先将其悬停。

    例如

    from selenium.webdriver.common.action_chains import ActionChains
    
    action_chain = ActionChains(driver)
    hover = driver.find_element_by_xpath("// span[contains(text(),'Share & Export')]")
    action_chain.move_to_element(hover).perform()      # hover to show drop down list
    driver.execute_script("window.scrollTo(0, 200)")   # scroll down a bit
    time.sleep(1)                                      # wait for scrolling
    button = driver.find_element_by_xpath("// button[contains(text(),'Get table as CSV (for Excel)')]")
    action_chain.move_to_element(button).click().perform()  # move to button and click
    time.sleep(3)
    

    输出:

    【讨论】:

    • actions.move_to_element(element).perform() 我收到MoveTargetOutOfBoundsException: Message: move target out of bounds
    • 这很奇怪。我跑得很完美。所以也许你需要在actions.move_to_element(element).perform() 之前移动driver.execute_script("window.scrollTo(0, 200)")time.sleep(1),以便在悬停之前向下滚动。
    【解决方案2】:

    这有时也发生在我身上。解决此问题的一种方法是获取此按钮的 X 和 Y 坐标并单击它。

    import pyautogui
        for element  in elements:
            element_pos = element.location
            element_size = element.size
            x_coordinate, y_coordinate = elemnt_pos['x'], element_pos['y']
            e_width, e_height = element_size['width'], element_size['height']
            click_x = x_coordinate + e_width/2
            click_y = y_coordinate + e_height/2
            pyauotgui.click(click_x, click_y)
    

    您可以尝试的其他解决方案是单击包含此按钮的标签。

    【讨论】:

      【解决方案3】:

      这里有几个问题:

      1. 您必须单击并打开共享和导出选项卡,然后单击将表格获取为 CSV 按钮
      2. 您必须滚动页面才能访问非第一个表。
        所以,你的代码可以是这样的:
      import pandas as pd
      import bs4
      from selenium import webdriver
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.action_chains import ActionChains
      
      from selenium.webdriver.chrome.options import Options
      import time
      import os
      
      #activate adblock plus
      path_to_extension = '/home/andreas/.config/google-chrome/Default/Extensions/cfhdojbkjhnklbpkdaibdccddilifddb/3.11.4_0'
      options = Options()
      options.add_argument('load-extension=' + path_to_extension)
      options.add_argument("window-size=1920,1080")
      
      #uses Chrome driver in usr/bin/ from https://chromedriver.chromium.org/downloads
      driver = webdriver.Chrome(options=options)
      actions = ActionChains(driver)
      
      #wait and switching back to tab with desired source
      time.sleep(5) 
      #driver.switch_to.window(driver.window_handles[0])
      
      NO_OF_PREV_SEASONS = 5
      
      df = pd.DataFrame()
      
      urls = ['https://fbref.com/en/squads/247c4b67/Arminia-Stats']
      
      for url in urls:
          driver.get(url)
          html = driver.page_source
          soup = bs4.BeautifulSoup(html, 'html.parser')
      
          #click button -> accept cookies
          element = driver.find_element(By.XPATH, '//button[text()="AGREE"]')
          element.click()
          
          for i in range(NO_OF_PREV_SEASONS):
              elements = driver.find_elements(By.XPATH, "//div[@class='section_heading_text']//li[@class='hasmore']")
              for element  in elements:
                  actions.move_to_element(element).perform()
                  time.sleep(0.5)
                  element.click()
                  wait.until(EC.visibility_of_element_located((By.XPATH, "//button[@tip='Get a link directly to this table on this page']"))).click()
      
                  #todo: get data
          
      
      

      【讨论】:

      • 这里也一样:actions.move_to_element(element).perform() 我收到了MoveTargetOutOfBoundsException: Message: move target out of bounds
      • 那不一样。您在哪个迭代中看到此错误?
      • 对不起,我的意思是上面提供的解决方案。奇怪的是它发生在第一次迭代?!
      • 好的,尝试将窗口大小设置为我在此处设置的足够大。让我知道现在它是否适用于第一次迭代
      • 我也不确定你应该在这里使用driver.switch_to.window(driver.window_handles[0])
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-23
      • 2017-08-18
      • 1970-01-01
      • 1970-01-01
      • 2019-05-16
      • 2022-08-02
      • 1970-01-01
      相关资源
      最近更新 更多