【问题标题】:Finding button by XPath with Selenium使用 Selenium 通过 XPath 查找按钮
【发布时间】:2021-09-17 07:29:36
【问题描述】:

我一直在尝试使用 Selenium WebDriver 找到以下按钮:


<div class="_1gw6tte">
    <div aria-hidden="false">
        <div class="_159vfsnv" style="background-color: transparent;">
            <div class="_12to336">
                <div class="_1ibtygfe">
                    <button type="button" class="_pmpl8qu">
                    Afficher plus de résultats</button>
                </div>
            </div>
        </div>
    </div>
</div>

我使用了 css 选择器、xpath、类,但似乎没有任何效果(即使只是复制粘贴检查员给出的那个。我最接近的是使用类 _1ibtygfe 定位 div) 这是我尝试过的所有内容,我很绝望,我不明白为什么它找不到它,它一直向我抛出 No such Element 异常。

wd.get(url)
print(wd.current_url) #check
#wd.find_element_by_xpath('//[@id="ExploreLayoutController"]/div[1]/div[2]/div[1]')#/div/div/div/button') #xpath given by inspector
#wd.find_element_by_class_name('_pmpl8qu')
#wd.find_element_by_xpath("//div[@class = '_1ibtygfe']/button[@class = '_pmpl8qu']")
#wd.find_element_by_xpath("//div[@class = '_1ibtygfe']/button")
#wd.find_elements_by_css_selector('button._pmpl8qu')

这是 airbnb 网站,我正在尝试抓取他们的活动并点击查看更多按钮。感谢您的帮助。

【问题讨论】:

  • 你能分享一个指向那个页面的链接吗?
  • 这是 airbnb 网站,我正在尝试抓取他们的活动并点击查看更多按钮 - 你能分享确切的 URL,我相信它是公开的。事件是什么意思?

标签: python html selenium web-scraping button


【解决方案1】:

如果你想点击这个按钮,你需要

  1. 关闭“接受 cookie”
  2. 滚动到该按钮,因为 in 超出了可见屏幕区域。
  3. 点击它
    如下:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
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.webdriver.common.action_chains import ActionChains
import time

wait = WebDriverWait(driver, 20)
actions = ActionChains(driver)

driver.get("https://fr.airbnb.be/s/Montreal--QC/experiences?tab_id=experience_tab&refinement_paths%5B%5D=%2Fexperiences&flexible_trip_dates%5B%5D=june&flexible_trip_dates%5B%5D=may&flexible_trip_lengths%5B%5D=weekend_trip&date_picker_type=calendar&source=structured_search_input_header&search_type=filter_change&rank_mode=default&place_id=ChIJDbdkHFQayUwR7-8fITgxTmU&checkin=2021-05-22")

wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button[data-testid="accept-btn"]"))).click()

button = driver.find_element_by_xpath("//button[contains(text(),'Afficher plus de résultats')]")
actions.move_to_element(button).build().perform()
time.sleep(0.5)
button.click()


【讨论】:

  • 也不行,什么是iframe?这是带有按钮的页面fr.airbnb.be/s/Montreal--QC/…
  • 好的,我明白了。你想点击那个按钮吗?
  • 请查看更新的代码是否有效。它应该
  • 嘿,感谢您的编辑,但不幸的是它仍然无法正常工作,在尝试通过路径。我不认为将其滚动到视图中是问题,我曾尝试删除该按钮下方的信息,但我没有遇到任何问题......
  • 原来我在关闭 cookie 和找到按钮之间添加了时间睡眠,现在它可以工作了!!!非常感谢
【解决方案2】:

看看这是否有效。

driver.find_element_by_xpath("//button[@data-testid='accept-btn']").click()

driver.find_element_by_xpath("//button[text()='Afficher plus de résultats']").click()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-01
    相关资源
    最近更新 更多