简体   繁体   中英

Selecting a button, list object has no attribute click python selenium

After searching for a while an answer to my question, I couldn't get an answer that helped me, so here I am asking for your help ! :)

Right now, I am trying to select a plan on a website page which, after it has been selected (Read : a certain button clicked) displays the rest of the page where I can send the keys / values that I want to send.

Here is the code I am using

select_plan = browser.find_elements_by_xpath(".//*[@id='PostAdMainForm']/div[1]/div/div/div/div/div[1]/div[3]/button")
select_plan.click()

I found the xpath with Firepath, but when I run my code it gives me a AttributeError: 'list' object has no attribute 'click'

Here is the page I am trying to click from

https://www.kijiji.ca/p-post-ad.html?categoryId=214&hpGalleryAddOn=false&postAs=ownr

(I am looking to click on the left button, the one in blue)

Thank you very much for you help :)

The method find_element s returns a list , not a single element. You are taking the result and trying to click on it. Like the error says, you can't click on a list.

Either use find_element (singular) or use find_elements (plural) and then click on one of the elements that was returned.

# using find_elements
select_plans = browser.find_elements_by_xpath(".//*[@id='PostAdMainForm']/div[1]/div/div/div/div/div[1]/div[3]/button")
if len(select_plans) > 0:
    select_plans[0].click()

# using find_element
select_plan = browser.find_element_by_xpath(".//*[@id='PostAdMainForm']/div[1]/div/div/div/div/div[1]/div[3]/button")
if select_plan:
    select_plan.click()

Though, the link for the page you shared did not have the blue button. However, I have found it, after navigating to 'Post Your Ad' page. You can click on the Select button which is in blue color using the text appearing before it. For example, using text Basic , you can reach to the Select button. Following code shows, how we can achieve this:

select_plan = browser.find_element_by_xpath("//h3[text()='Basic']/following::button[text()='Select'][1]")
select_plan.click()

Let me know, whether it works for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM