【问题标题】:Selenium Chrome, Python. Couldn't find the button on the website硒铬,Python。在网站上找不到按钮
【发布时间】:2016-10-05 03:37:53
【问题描述】:

我刚刚学习如何使用 Webdriver,当我在 Eventbrite 网站上玩它时,它在那里找不到创建事件按钮(我可以点击它)。我尝试了很多不同的方法来定位按钮,例如 XPath、link_text、class,但它们都不起作用。这是我正在使用的代码:

    driver = self.driver
    driver.get("https://www.eventbrite.com/")
    driver.find_element_by_xpath("(//a[contains(text(),'Log in')])[2]").click()

    driver.find_element_by_id("login-email").clear()
    driver.find_element_by_id("login-email").send_keys("email")
    driver.find_element_by_id("login-password").clear()
    driver.find_element_by_id("login-password").send_keys("password")
    driver.find_element_by_id("remember_me").click()
    driver.find_element_by_xpath("//input[@value='Log in']").click()
    time.sleep(2)
    driver.find_element_by_link_text("Create Event").click()

谁能帮我解决这个问题?

【问题讨论】:

    标签: python macos google-chrome selenium


    【解决方案1】:

    如果我正确理解您的问题,那么您示例的最后一行 ("Create Event") 是行不通的吗?我可以说,如果我使用 CSS 选择器,我可以单击该按钮。 (我也倾向于发现 css 选择器更容易使用)

    driver = self.driver
    driver.get("https://www.eventbrite.com/")
    driver.find_element_by_xpath("(//a[contains(text(),'Log in')])[2]").click()
    
    driver.find_element_by_id("login-email").clear()
    driver.find_element_by_id("login-email").send_keys("email")
    driver.find_element_by_id("login-password").clear()
    driver.find_element_by_id("login-password").send_keys("password")
    driver.find_element_by_id("remember_me").click()
    driver.find_element_by_xpath("//input[@value='Log in']").click()
    time.sleep(2)
    driver.find_element_by_css_selector("div.l-mar-top-3 a.js-organizer-cta-btn").click()
    

    【讨论】:

    • 感谢您的建议。对于我未来的工作,我将使用 css 选择器,它可以更精确地感谢链接文本。但是当我尝试您的解决方案时,我得到了 AttributeError: 'list' object has no attribute 'click'。
    • 你是对的,它应该是单数find_element_by_css_selector,而不是复数版本。现已更新。
    • 看起来是因为您使用了大写字符串(“Create Event”),而实际的按钮文本全部大写(“CREATE EVENT”)。
    猜你喜欢
    • 1970-01-01
    • 2018-03-09
    • 2021-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-06
    相关资源
    最近更新 更多