将Selenium引用到javascript按钮时遇到问题.[python]

Having trouble referring Selenium to a javascript button. [python]

本文关键字:问题 遇到 python 按钮 Selenium 引用 javascript      更新时间:2023-09-26

如果存在回复按钮,则编写Python脚本从craigslist帖子中抓取电子邮件地址。然而,我在通过Selenium点击craigslist发布页面上的"回复"javascript按钮时遇到了问题。这是我的:

def clist():
        i = 'http://sfbay.craigslist.org/eby/ret/5344993908.html'
        driver.get(i)
        reply = driver.find_element_by_class_name("button.reply_button.js-only")
        reply.click()

edit:我还尝试过使用相同选择器的driver.find_element_by_css_selector,以及使用xpath作为''buttondriver.find_element_by_xpath。所有人都抛出了类似的错误。

实际输出:

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"css selector","selector":"button.reply_button.js-only"}

我所期望的:让脚本真正点击回复按钮。

reply_button只有一个按钮,因此可以简化类选择器。

from selenium import webdriver
browser = webdriver.Firefox()

def clist():
    url = 'http://sfbay.craigslist.org/eby/ret/5344993908.html'
    browser.get(url)
    reply = browser.find_element_by_class_name('reply_button')
    reply.click()
clist()