【问题标题】:Python Selenium cant find loadmore (mehr anzeigen) buttonPython Selenium 找不到加载更多(mehr anzeigen)按钮
【发布时间】:2021-06-24 05:19:57
【问题描述】:

我基本上想在运行其余代码之前单击页面上的每个加载更多按钮,否则我将无法访问每个配置文件。

有两个问题:

首先我如何访问它?我尝试了与我的代码的 fancyCompLabel 部分类似的方法,但它不起作用。

第二个我不确定我应该如何遍历所有按钮,因为我会假设第二个按钮仅在单击第一个按钮之前才开始加载。

这是相关的html部分和按钮的图片

<span type="button" class="md-text-button button-orange-white" onclick="loadFollowing();">mehr anzeigen</span>

这是访问每个配置文件的代码,但您可以看到它只运行到第一个加载按钮。


from bs4 import BeautifulSoup
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

time.sleep(3)

# Set some Selenium Options
options = webdriver.ChromeOptions()
# options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')

# Webdriver
wd = webdriver.Chrome(executable_path='/usr/bin/chromedriver', options=options)
# URL
url = 'https://www.techpilot.de/zulieferer-suchen?laserschneiden%202d%20(laserstrahlschneiden)'

# Load URL
wd.get(url)

# Get HTML
soup = BeautifulSoup(wd.page_source, 'html.parser')

wait = WebDriverWait(wd, 15)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#bodyJSP #CybotCookiebotDialogBodyLevelButtonLevelOptinAllowAll"))).click()
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "#efficientSearchIframe")))
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".hideFunctionalScrollbar #CybotCookiebotDialogBodyLevelButtonLevelOptinAllowAll"))).click()
#wd.switch_to.default_content()  # you do not need to switch to default content because iframe is closed already
wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".fancyCompLabel")))

results = wd.find_elements_by_css_selector(".fancyCompLabel")

for profil in results:
   print(profil.text) #heres the rest of my code but its not relevant 

wd.close()

【问题讨论】:

    标签: python html selenium


    【解决方案1】:

    我看到.hideFunctionalScrollbar #CybotCookiebotDialogBodyLevelButtonLevelOptinAllowAll 定位的第二个弹出元素最初出现在可见屏幕之外。
    因此,切换到iframe 后,您需要滚动到该元素,然后再尝试单击它。
    此外,presence_of_all_elements_located 实际上并不等待所有元素的存在。它甚至不知道会有多少这样的元素。一旦找到至少 1 个与传递的定位器匹配的元素,它就会返回。
    所以我建议在该行之后添加一个短暂的睡眠,以允许实际加载所有这些元素。

    from selenium.webdriver.common.action_chains import ActionChains
    
    wait = WebDriverWait(wd, 15)
    actions = ActionChains(wd)
    
    wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#bodyJSP #CybotCookiebotDialogBodyLevelButtonLevelOptinAllowAll"))).click()
    wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "#efficientSearchIframe")))
    
    second_pop_up = wd.find_element_by_css(".hideFunctionalScrollbar #CybotCookiebotDialogBodyLevelButtonLevelOptinAllowAll")
    actions.move_to_element(second_pop_up).build().perform()
    time.sleep(0.5)
    second_pop_up.click()
    
    wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".fancyCompLabel")))
    time.sleep(0.5)
    
    for profil in results:
       print(profil.text) #heres the rest of my code but its not relevant 
    
    wd.close()
    

    【讨论】:

      猜你喜欢
      • 2021-12-08
      • 1970-01-01
      • 2022-11-12
      • 1970-01-01
      • 2021-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多