我已经try 了许多不同的方法,我发现在网上如何找到这个按钮,但每次try 后,功能给我一个空的list.

我需要找到按钮,并点击它,以刮不同的页面.整个页面是动态加载的,第二个页面的内容直到您打开它才会加载,这意味着它们不在DOM中,直到您移动到第二个页面.这些页面也是动态的,这意味着如果你点击不同的页面,URL不会改变.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from bs4 import BeautifulSoup
import time
from selenium.webdriver.common.by import By

# Create a new instance of the Chrome driver
driver = webdriver.Chrome()

wait = WebDriverWait(driver, 10)

# Go to the webpage
driver.get('https://is.muni.cz/predmet/?volby=obory:4382@fakulta:1433@obdobi:podzim%202023,jaro%202024@jazyky:eng')


links = []

driver.implicitly_wait(15)

for i in range(1):
    
    website = driver.page_source
    soup = BeautifulSoup(website, 'html.parser')

    links += ['https://is.muni.cz' + link['href'] for link in soup.find_all('a', class_='course_link')]

    button = driver.find_elements(By.XPATH, '//a[@class="isi-zobacek-vpravo isi-inline"]')
    button.click()
    time.sleep(5)
    i += 1

print(links)

driver.quit()

这段代码只返回一个错误,因为单击函数不起作用,因为按钮没有内容.这是一个空的list.

推荐答案

第一个问题-你试图找到多个元素,这将产生list,但这些元素没有方法点击.

第二个问题-您的 Select ,没有a有这样的class你试图寻找.

因此,请将您的 Select 更改为:

driver.find_element(By.XPATH, '//li[@class=" pagination-next"]/a')

为了以防万一,请查看expliced waitstry / except的概念,并可能使用while loop来迭代页面:

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

# Create a new instance of the Chrome driver
driver = webdriver.Chrome()

# Go to the webpage
driver.get('https://is.muni.cz/predmet/?volby=obory:4382@fakulta:1433@obdobi:podzim%202023,jaro%202024@jazyky:eng')

links = []

while True:
    
    website = driver.page_source
    soup = BeautifulSoup(website, 'html.parser')

    links.extend(['https://is.muni.cz' + link['href'] for link in soup.find_all('a', class_='course_link')])
    
    try:
        WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//li[@class=" pagination-next"]/a'))).click()
    except TimeoutException:
        break

print(links)

driver.quit()

Python相关问答推荐

DataFrame groupby函数从列返回数组而不是值

从dict的列中分钟

如何在Polars中从列表中的所有 struct 中 Select 字段?

根据列值添加时区

转换为浮点,pandas字符串列,混合千和十进制分隔符

未知依赖项pin—1阻止conda安装""

使用groupby方法移除公共子字符串

try 检索blob名称列表时出现错误填充错误""

下三角形掩码与seaborn clustermap bug

在pandas/python中计数嵌套类别

为什么常规操作不以其就地对应操作为基础?

如何从pandas DataFrame中获取. groupby()和. agg()之后的子列?

如何设置nan值为numpy数组多条件

SpaCy:Regex模式在基于规则的匹配器中不起作用

为什么我只用exec()函数运行了一次文件,而Python却运行了两次?

一维不匹配两个数组上的广义ufunc

通过对列的其余部分进行采样,在Polars DataFrame中填充_null`?

如何在不不断遇到ChromeDriver版本错误的情况下使用Selify?

对当前的鼹鼠进行编码,并且我的按键获得了注册

了解如何让库认识到我具有所需的依赖项