按钮点击和网页抓取

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

我试图从以下位置获取评论数量和评论内容:https://www.arlnow.com/ 我的“加载更多评论”按钮有问题。 我试过:

load_more_xpath = '//a[contains(text(), "Load more comments")]'
load_more_button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, load_more_xpath)))
load_more_button.click()

我尝试了不同的方法,但找不到正确的 findelement 来获取评论数量和评论内容!

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

假设这是您要单击的元素:

enter image description here

目标元素(加载更多评论)包含在IFRAME中。在这种情况下,您需要首先在

IFRAME
内切换驱动程序上下文,然后执行所需的操作。

请参阅下面的代码切换到

iframe
,然后单击目标按钮。

wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "(//iframe[contains(@id,'dsq-app')])[1]")))

load_more_xpath = '//a[contains(text(), "Load more comments")]'
load_more_button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, load_more_xpath)))
load_more_button.click()

# Below line will come out of IFRAME context
driver.switch_to.default_content()
© www.soinside.com 2019 - 2024. All rights reserved.