I am very new to Python and Selenium. I am using chromedriver. I need some help. I have a web page unfortunately I cannot show it because it's blocked to be opened from everyone but locally at work. Basically it is a web page where there are around 15 buttons (same class) and when clicked the button disappears. I want to make a code that will click on every single button one by one until they are all clicked (disappeared). Then refresh the page and do the same for the next 100 pages.
2 Answers
i don't have your page to test this code so let's hope it works..
you can get all buttons in page within a list and then make a loop to click all list items .. it should looks like that:
# this will return a list with all buttons buttons = driver.find_elements_by_xpath("//*[@class='btn default check check green markAsChecked']") # clicking loop for button in buttons: button.click()
you may need to add a small delay between clicks because it bugs sometime in selenium.. just try first without delay .. if it doesn't work add sleep(0.5)
in loop or make it click with javascript
- If there is a page refresh after button click, all your elements is your array get stale.– TrapliCommentedOct 18, 2019 at 11:28
- he said in comments that page is not refreshed and button clicks doesn't affect the pageCommentedOct 18, 2019 at 11:54
- That is an assumption from the OP. I wouldn't rely on that.So far I haven't seen a single line of his code or the page source. The button disappears. Is it hidden by css, removed by js, or is it a page refresh? We don't know.– TrapliCommentedOct 18, 2019 at 12:04
- yes you're right .. that's why we need the page source at leastCommentedOct 18, 2019 at 12:11
What I found to work for me is this, I found similar question here and one of the answers was this. Not sure how actually this works and why I have to use specific numbers for buttons because if I use buttons[4].click() it won't work.
buttons = driver.find_elements_by_xpath("//*[@class='btn default check check green markAsChecked']") buttons[2].click() buttons = driver.find_elements_by_xpath("//*[@class='btn default check check green markAsChecked']") buttons[3].click() buttons = driver.find_elements_by_xpath("//*[@class='btn default check check green markAsChecked']") buttons[5].click() buttons = driver.find_elements_by_xpath("//*[@class='btn default check check green markAsChecked']") buttons[7].click()