【问题标题】:Selenium WebDriver Can't Click ButtonSelenium WebDriver 无法单击按钮
【发布时间】:2021-01-28 15:15:14
【问题描述】:

我正在尝试自动化 Etsy 上的消息传递系统,但我的代码遇到了问题。我收到找不到 reCaptcha 元素的错误消息,我认为这意味着我的代码一开始没有成功单击消息按钮。

我尝试过使用 xpath、id、类名等,但我仍然不知道如何单击“消息按钮”。

这是Messaging ButtonMessaging Interface

url = 'https://www.etsy.com/sg-en/listing/794585057/lavender-chamomile-shower-steamers-sleep?ga_order=most_relevant&ga_search_type=all&ga_view_type=gallery&ga_search_query=green+beauty&ref=sc_gallery-1-2&plkey=2bab0193d5d13095397b2acdab033b78a0d0f30b%3A794585057&cns=1'
wd.get(url)
last_height = wd.execute_script("return document.documentElement.scrollHeight;")  

time.sleep(3)

#loading the page
wd.execute_script('window.scrollTo(0, arguments[0]);',last_height/3)
time.sleep(3)
wd.execute_script('window.scrollTo(0, arguments[0]);',last_height/3 + last_height/3)
time.sleep(3)
wd.execute_script('window.scrollTo(0, arguments[0]);',last_height)
time.sleep(3)

#clicking on the "Message Seller" button 
html = wd.page_source
wd.find_element_by_xpath("//*[@id='desktop_shop_owners_parent']/div/a").click()
time.sleep(3)

#clicking on the reCaptcha button
wd.find_element_by_id("recaptcha-anchor").click()

time.sleep(1)
 
#typing in a test message 
open_message = wd.find_element_by_xpath("//*[@id='chat-ui-composer']/div[1]/div[1]/textarea").click()
open_message.sendKeys('hi (-: ')
wd.find_element_by_xpath("//*[@id='chat-ui composer']/div[1]/div[2]/button").click()


#marker to show that message has been sent
brand = wd.find_element_by_xpath("//*[@id='desktop_shop_owners_parent']/div/div/div[2]/p[1]").text
print('messaged: ' + brand)

wd.quit()

非常感谢任何帮助!

【问题讨论】:

    标签: python selenium selenium-webdriver button


    【解决方案1】:
    wd.find_element_by_css_selector('[class="wt-btn wt-btn--filled wt-mb-xs-0"]').click()
    wd.find_element_by_xpath(
        "//*[@id='desktop_shop_owners_parent']/div/a").click()
    

    您正在使用 find_elements ,将其更改为 find element 也必须关闭接受cookie弹出

    完整代码:

    url = 'https://www.etsy.com/sg-en/listing/539990072/chamomile-flowers-matraurea-umbellatum?ref=sold_out-10'
    wd.get(url)
    last_height = wd.execute_script(
        "return document.documentElement.scrollHeight;")
    
    time.sleep(3)
    
    #loading the page
    wd.execute_script('window.scrollTo(0, arguments[0]);', last_height/3)
    time.sleep(3)
    wd.execute_script(
        'window.scrollTo(0, arguments[0]);', last_height/3 + last_height/3)
    time.sleep(3)
    wd.execute_script('window.scrollTo(0, arguments[0]);', last_height)
    time.sleep(3)
    
    #clicking on the "Message Seller" button
    html = wd.page_source
    wd.find_element_by_css_selector(
        '[class="wt-btn wt-btn--filled wt-mb-xs-0"]').click()
    
    time.sleep(4)
    wd.find_element_by_xpath("//*[@id='desktop_shop_owners_parent']/div/a").click()
    time.sleep(3)
    

    【讨论】:

    • 谢谢!更改了它,但我仍然找不到元素 - 有什么想法吗?
    • @codinggirl 因为该商品已售出! .我为另一个项目添加了完整代码尝试一下
    • 非常感谢您的帮助!仍然不工作)-:
    • 在这段代码之前它可以工作,请再试一次
    猜你喜欢
    • 2016-03-02
    • 2014-01-04
    • 1970-01-01
    • 1970-01-01
    • 2017-01-17
    • 2016-11-26
    • 2021-11-09
    • 2022-12-05
    • 1970-01-01
    相关资源
    最近更新 更多