无法使用 Selenium 访问/单击按钮

问题描述 投票:0回答:1

在以下网站 Forge 上,我需要多次访问“加载更多”按钮(位于组件列表底部),以抓取可用组件的列表。虽然我能够访问我需要的任何元素,但我很难直接访问按钮然后单击它。

使用时

button = browser.find_element(By.XPATH, '//*[@id="PortalTheme_wt778_block_wtMainContent_wtLoadMore"]')
button.click()

我得到了元素。但是,我无法点击它。

我没有网络开发经验,而且我对网络抓取很陌生。因此,我正在努力找出这里到底出了什么问题。

如果有人能告诉我如何单击此按钮,我将不胜感激!

提前致谢:)

python selenium-webdriver button
1个回答
0
投票

您需要等待,直到准备好单击加载更多。使用 Selenium 的 Waits 对所需按钮执行操作。

参考下面的代码,将使用

Explicit Waits
单击加载更多

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

driver =  webdriver.Chrome()
driver.get("https://www.outsystems.com/forge/list?q=&t=&o=most-popular&tr=False&oss=False&c=%205361,5362,5363,5364,5365,5366,5367,5368,5369,5370,5381,5382,5383,5384,5385,5386,5387,5388,5389,5390,5391,3485,5392,5393&a=&v=&hd=False&tn=&scat=forge")
driver.maximize_window()

# Create wait object to wait for 20s
wait = WebDriverWait(driver,20)

# Click on Accept cookies button
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[@id='onetrust-accept-btn-handler']"))).click()

# Click on Load more button
wait.until(EC.element_to_be_clickable((By.XPATH, "//*[@id='PortalTheme_wt778_block_wtMainContent_wtLoadMore']"))).click()
time.sleep(10)
© www.soinside.com 2019 - 2024. All rights reserved.