簡體   English   中英

Selenium WebDriver 無法點擊按鈕

[英]Selenium WebDriver Can't Click Button

我正在嘗試在 Etsy 上實現消息傳遞系統的自動化,但我的代碼遇到了麻煩。 我收到無法找到 reCaptcha 元素的錯誤消息,我認為這意味着我的代碼首先沒有成功單擊消息傳遞按鈕。

我試過使用 xpath、id、class 等名稱,但我仍然不知道如何單擊“消息按鈕”。

這是消息按鈕消息界面

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()

非常感謝任何幫助!

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 也必須關閉 accept cookies 彈出窗口

完整代碼:

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM