Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
435 views
in Technique[技术] by (71.8m points)

python - Selenium WebDriver Can't Click Button

I'm trying to automate a messaging system on Etsy and am running into trouble with my code. I receive error messages that the reCaptcha element can't be found, which I think means that my code didn't successfully click the messaging button in the first place.

I've tried using xpath, id, class names, etc but I still can't figure out how to click the "message button".

here's the Messaging Button and the Messaging 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()

Would really appreciate any help!

question from:https://stackoverflow.com/questions/65939965/selenium-webdriver-cant-click-button

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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()

you are using find_elements , change it to find element also have to close the accept cookies pop up

Full code:

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...