无法在python中使用selenium激活网页的隐藏部分

2024-06-17 11:57:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在python中使用selenium从AllTrails.com/us/florida/state-parks提取数据。我需要的是录音资料。当您第一次打开该网站时,它看起来像下图,并且“评论”处于活动状态

Webpage Visual

感兴趣部分的html代码为:

webpage HTML

现在你可以在html中看到“评论”是活动的(我需要活动的录音)

当我实际单击“录制”时,它会激活录制,但当我尝试使用selenium虚拟地“单击”它时,它不会给出错误,但它不会被激活,代码的后续部分也不会工作。我的代码的相关部分如下:

def get_trail_links (driver, trails_url):
    time.sleep(1)
    driver.get(trails_url)
    page_count = 0

#here is where i am trying to click on the "recordings" button, no error happens here.
    driver.find_element_by_xpath("//h3[a/@name='Recordings']").click()

    recordings = driver.find_elements_by_xpath("//*[@id ='tracks']")
    while page_count < 2:
        for record in recordings:

#here is where my button won't work because the recordings is not active in the html.

            button = driver.find_element_by_xpath("//button[@title='Show more recordings']")
            button.click()
            time.sleep(1)
            page_count += 1

Tags: the代码byhereishtmldriverselenium
1条回答
网友
1楼 · 发布于 2024-06-17 11:57:38

使用此代码。您还可以通过更改while循环中的值来更改按钮单击的次数

import time

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

driver = webdriver.Chrome(executable_path= r"D:\Selenium Learning\chromedriver_win32 (2)\chromedriver.exe")
driver.get('https://www.alltrails.com/us/florida/state-parks')


wait = WebDriverWait(driver, 5000)
wait.until(EC.element_to_be_clickable((By.XPATH, "//div[contains(@class,'row trail-row')]/div/div[3]")))
driver.find_element_by_xpath("//div[contains(@class,'row trail-row')]/div/div[3]").click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//*[@id='tracks']/div[1]/div[1]/div[2]/div[1]/div[1]/h4")))


page_count=0
while page_count < 2:
    button = driver.find_element_by_xpath("//button[@title='Show more recordings']")
    button.click()
    time.sleep(1)
    page_count += 1
recordings = driver.find_elements_by_xpath("//*[@id='tracks']/div/div/div/div/div/h4")
for recording in recordings:
    print(recording.text)

PS:执行时,请将可执行路径更改为本地chrome驱动程序路径

相关问题 更多 >