Home > Enterprise >  Selenium webdriver click in more than one button at same page
Selenium webdriver click in more than one button at same page

Time:11-09

I'm trying to use selenium webdriver in python to click in all the thanks button on a page, but the problem is that my script is only clicking on the first button.

Below is the part of code that I´m using:

counter = 0
while counter < 10:
    wd.find_element_by_xpath('//*[contains(@href,"post_thanks.php?do=")]').click()
    print ("click")
    counter  = 1
    time.sleep(2)
wd.close()

The script is working, it's connecting to the website, loading the target page, but only clicks at the first thanks button.

What can be made to get all the buttons clicked?

CodePudding user response:

Try:

wd.find_element_by_xpath('(//*[contains(@href,"post_thanks.php?do=")])[1]').click()
time.sleep(2)
wd.find_element_by_xpath('(//*[contains(@href,"post_thanks.php?do=")])[2]').click()

I do not know why you have the for loop, but as you can see with those xpath we identify the 2 different buttons

Without sharing the html this is the best approach I can think.

CodePudding user response:

thanks = wd.find_elements_by_xpath('//*[contains(@href,"post_thanks.php?do=")]')
for ta in thanks:
     ta.click()
     time.sleep()
  • Related