gpt4 book ai didi

python selenium点击按钮xpath错误

转载 作者:太空宇宙 更新时间:2023-11-03 13:14:17 24 4
gpt4 key购买 nike

我正在尝试抓取 airbnb 列表。除了单击“更多”之外,我想不出一种方法来获取完整的便利设施列表。我正在使用 selenium 来模拟点击,但它似乎不起作用。

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains

url = 'https://www.airbnb.com/rooms/4660676'
driver = webdriver.Firefox()
driver.get(url)
elem = driver.find_element_by_xpath('//a[@class="expandable-trigger-more"]')
actions.click(elem).perform()

最佳答案

XPath 本身是正确的,但您还没有定义actions:

from selenium.webdriver.common.action_chains import ActionChains

elem = driver.find_element_by_xpath('//a[@class="expandable-trigger-more"]')

actions = ActionChains(driver)
actions.click(elem).perform()

请注意,您可以简单地使用 WebElementclick() 方法:

elem = driver.find_element_by_xpath('//a[@class="expandable-trigger-more"]')
elem.click()

对我有用。


如果您收到 NoSuchElementException 错误,您可能需要等待链接可通过 WebDriverWait 进行点击和 element_to_be_clickable

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

element = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, '//a[@class="expandable-trigger-more"]'))
)
element.click()

关于python selenium点击按钮xpath错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34951032/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com