简体   繁体   中英

Click multiple buttons with Selenium on Python

I am trying to click multiple buttons with Selenium ChromeDriver, should be easy, I just find the buttons by class name because they all have the same class name, looping through each one and clicking. However for some reason, if I check the len of the Buttons it is 0 it is like it's not on the page...

Here is the HTML:

button1

<span data-fieldid="9537" id="bounceRatingOrderBtn" name="bounceRatingOrderBtn" class="viewCommand viewSize1 viewCommandGreenBtn" data-columnnum="1" data-showonload="1" data-defaultvalue="" data-isrequired="0" data-personalareaviewposition="manage_area" data-ispersonalareaviewable="1" data-ajax_path="//my.yad2.co.il/newOrder/index.php?action=updateBounceListing&amp;CatID=3&amp;SubCatID=0&amp;OrderID=39605635" data-viewcommandactive="1" data-originalelementname="input" xpath="1">  <i class="fa fa-arrow-up"></i> <span>הקפצת מודעה</span></span>

button2

<span data-fieldid="9537" id="bounceRatingOrderBtn" name="bounceRatingOrderBtn" class="viewCommand viewSize1 viewCommandGreenBtn" data-columnnum="1" data-showonload="1" data-defaultvalue="" data-isrequired="0" data-personalareaviewposition="manage_area" data-ispersonalareaviewable="1" data-ajax_path="//my.yad2.co.il/newOrder/index.php?action=updateBounceListing&amp;CatID=3&amp;SubCatID=0&amp;OrderID=39605688" data-viewcommandactive="1" data-originalelementname="input" xpath="1">  <i class="fa fa-arrow-up"></i> <span>הקפצת מודעה</span></span>

button3

<span data-fieldid="9537" id="bounceRatingOrderBtn" name="bounceRatingOrderBtn" class="viewCommand viewSize1 viewCommandGreenBtn" data-columnnum="1" data-showonload="1" data-defaultvalue="" data-isrequired="0" data-personalareaviewposition="manage_area" data-ispersonalareaviewable="1" data-ajax_path="//my.yad2.co.il/newOrder/index.php?action=updateBounceListing&amp;CatID=3&amp;SubCatID=0&amp;OrderID=39594079" data-viewcommandactive="1" data-originalelementname="input" xpath="1">  <i class="fa fa-arrow-up"></i> <span>הקפצת מודעה</span></span>

My Python code:

for i in driver.find_elements_by_class_name("viewCommand viewSize1 viewCommandGreenBtn"):
    print('here')
    i.click()

Compound classes are represented like this in CSS

classOne.classTwo.classThree {
    //...
}

Try doing

for btn in driver.find_elements_by_css_selector('viewCommand.viewSize1.viewCommandGreenBtn'):
    btn.click()

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