How to create loop in Python Selenium - python

I am very new so I would need some help. Can you help me create a loop for the following actions. Click on all buttons and refresh the page and then do it 100 more times for example.enter image description here
my code

Based on the minimal sample you provided, you can refactor this pretty easily. Here's how I would fix that:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
# wait on buttons
WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located(
(By.XPATH, "//*[#class='btn default check check green markAsChecked']")))
buttons = driver.find_elements_by_xpath("//*[#class='btn default check check green markAsChecked']")
# click buttons in a loop
for button in buttons:
button.click()
I added a WebDriverWait between buttons, as it's better practice to wait on elements before clicking them.

Related

How to open multiple button with same class?

In the website, I want to scrape there are different dropdown lists. I want to open before copying data.
I have structured the flow like this:
buttons = driver.find_elements(By.CSS_SELECTOR, 'svg[class = "undefined event__expander event__expander--close"]')
for button in buttons:
button.click()
But if I run it, I can open only the first list and the others are still closed.
Any tips?
driver.maximize_window()
wait=WebDriverWait(driver,20)
driver.get('https://www.diretta.it/')
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"button#onetrust-accept-btn-handler"))).click()
buttons = driver.find_elements(By.CSS_SELECTOR, 'svg[class = "undefined event__expander event__expander--close"]')
for button in buttons:
button.click()
It might have been due to the accept cookies but your code works after you click it.
Imports:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

How to make selenium python script to keep clicking a button?

Objective:
Run a continuous function to keep clicking a button on target url page with wait between clicks
url = "https://kingoftheclicks.com/?ref=ghost-of-a-chance"
target button = "+ Click button"
Later I will set it to target opponent "clicks - 1,000"
//*[#id="__layout"]/div/main/div[3]/div[3]/div/div[2]/div/div[2]/div[1]/div[3]/text()[2]
Need help getting this basic code to work and click button.
from selenium.webdriver.common.action_chains import ActionChains
elem = driver.find_element_by_xpath('//a#id="__layout"]/div/main/div[3]/div[3]/div/div[2]/div/div[2]/div[2]/div[2]/div[2]')
actions = ActionChains(driver)
actions.click(elem).perform()
You can use a loop to do an action over and over. For example, to print something 10 times:
for i in range(10):
print("foo")
You can also use while loops, whose syntax is something like:
while [some_condition_is_true]:
[do something]
If you need to wait between each click, you can sleep:
import time
time.sleep(0.1) #This will pause the program for 100 milliseconds
You first need a better locator! then you should use WebDriverWait.
As #Kosay suggested to use while here is an implementation:
driver.get("https://kingoftheclicks.com/?ref=ghost-of-a-chance")
WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.advance")))
driver.find_element(By.CSS_SELECTOR, "button.advance").click()
WebDriverWait(driver,30).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".fighter--challenger .fighter-btn")))
while driver.find_element(By.CSS_SELECTOR,".fighter--challenger .fighter-btn").is_displayed():
driver.find_element(By.CSS_SELECTOR, ".fighter--challenger .fighter-btn").click()
Note: I added WebDriverWait so you'll need to import it too.
Here are the imports:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

Why can't Xpath click the element?

I am Trying to create a bot that can fill a cart with these beer bottles. I really want to do this on a few different sites but for some reason i can only get it to open the page and click the first button, then it doesn't click the next button. I tried ID, Name, pretty much anyway to identify the button but it won't click it. I even tried sleep for 3 seconds. I tried to see if it was in an Iframe but i don't think it is. I'm out of ideas.... Link is https://www.sideprojectbrewing.com/shop?category=Beer+Release
I'm trying to access the add to cart element but does not seem to work
\
from Config import keys
from selenium import webdriver
def order(k):
driver = webdriver.Chrome(executable_path=r"C:\Users\ezliv\Desktop\ShopBot1\chromedriver_win32\chromedriver.exe")
driver.get(k['product_url'])
driver.find_element_by_xpath('//*[#id="thumb-biereblanche"]/div/div[1]/div/div/img').click()
driver.find_element_by_xpath('//*[#id="yui_3_17_2_1_1606181545139_755"]').click()
\\
You can try following code:
driver.get(url_here)
wait = WebDriverWait(driver, 20)
bottle = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[#id="thumb-biereblanche"]/div/div[1]/div/div/img')))
bottle.click()
add_to_cart = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'sqs-add-to-cart-button-inner')))
add_to_cart.click()
Import:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

Python Selenium closing a pop-up window

I'd like to close this window opened up on Amazon web site by using Selenium with Python. I've tried find_element_by_xpath, but it doesn't work. Here's the snippet of the code;
close_to_list = browser.get("/html/body/div[4]/div/div/div[2]/div[2]/div[2]/div[1]/div[2]/div/div/table/tbody/tr[2]")
I get the xpath of that 'X' button but I guess I need to close it as switch_to_alert, but I'm new to this era so I couldn't write it properly.
Here's the image view;
enter image description here
You don't need to use switch_to_alert() as well as get() to close modal window. Just try to close it with below code:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'button[aria-label="Close"]'))).click()
This allows you to wait for button appearance and click the button

Selenium click hidden button

I want to control webpage with selenium and python
Python code;
menu = browser.find_element_by_css_selector(".nav")
hidden = browser.find_element_by_link_text('Soluton')
element = browser.find_element_by_xpath("//div[#class=\"two-columns\"]")
Option 1: ActionChains(browser).move_to_element(menu).click(hidden)
Option 2 : ActionChains(browser).move_to_element(element).click(hidden)
html code;
I want to click "Solution" button under the nav menu.
However, Solution be in under the nav menu. So it's hidden.
so, I type following codes ;
 Option 1:
ActionChains(browser).move_to_element(menu).click(hidden)
 Option 2 :
ActionChains(browser).move_to_element(element).click(hidden)
But selenium not happen anything and not give any error message.
How can I click a button under nav menu with selenium ?
Thanks
If I have understood your Question and your Requirement we need to Mouse Hover over the WebElement with text as Actions where 2 options pops out as Request and Solution and you want to click() on Solution. To achieve that you can use the following code block :
#import
from selenium.webdriver.common.action_chains import ActionChains
#code block
menu = driver.find_element_by_xpath("//a[#class='incident-action']/i[#class='icon-ellipsis-h']")
ActionChains(driver).move_to_element(menu).perform()
driver.find_element_by_xpath("//div[#class='action-list' and #id='header-actions-list']//a[#class='icon-arrow' and contains(text(),'Solution')]").click()
You can try to click Actions to make actions list visible and then click on required Solution option:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
actions = wait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, "Actions")))
actions.click()
solution = wait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, "Solution")))
solution.click()

Categories