Is it possible to click multiply buttons with the same text with Selenium?
You can find all buttons by text and then execute click() method for each button in a for loop.
Using this SO answer it would be something like this:
buttons = driver.find_elements_by_xpath("//*[contains(text(), 'My Button')]")
for btn in buttons:
btn.click()
I also recommend you take a look at Splinter which is a nice wrapper for Selenium.
Splinter is an abstraction layer on top of existing browser automation
tools such as Selenium, PhantomJS and zope.testbrowser. It has a
high-level API that makes it easy to write automated tests of web
applications.
I had the following in html:
driver.find_element_by_xpath('//button[contains(text(), "HELLO")]').click()
To locate and click a <button> element by it's text you can use either of the following Locator Strategies:
Using xpath and text():
driver.find_element_by_xpath("//button[text()='button_text']").click()
Using xpath and contains():
driver.find_element_by_xpath("//button[contains(., 'button_text')]").click()
Ideally, to locate and click a <button> element by it's text you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using XPATH and text():
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='button_text']"))).click()
Using XPATH and contains():
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(., 'button_text')]"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Update
To locate all the <button> elements by the text you can use either of the following Locator Strategies:
Using xpath and text():
for button in driver.find_elements_by_xpath("//button[text()='button_text']"):
button.click()
Using xpath and contains():
for button in driver.find_elements_by_xpath("//button[contains(., 'button_text')]"):
button.click()
Ideally, to locate all the <button> elements by the text you need to induce WebDriverWait for the visibility_of_all_elements_located() and you can use either of the following Locator Strategies:
Using XPATH and text():
for button in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//button[text()='button_text']"))):
button.click()
Using XPATH and contains():
for button in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//button[contains(., 'button_text')]"))):
button.click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
#nobodyskiddy, Try to use driver.find_element ( if you have a single button option ), use index to click() when you are using driver.find_elements, find_elements will return array to web element values, so you have to use the index to select or click.
Related
I am trying to create a web application that automately republishes on a site for private sales.
Login and everything went well, but if I try to find and click the republish button it just wont work.
I've tried it with every locator, but the problem is that every button got an uniqe ID.
Example:
<button name="republish" type="button" data-testid="5484xxxxx-republish-button" class="Button__ButtonContainer-sc-3uxxxx-0 hxXxxX">Republish</button>
The last I've tried:
buttons = driver.find_elements(By.XPATH, "//*[contains(text(), 'Republish')]")
for btn in buttons:
btn.click()
But it also didnt work, same with By.NAME, BY.TAG_NAME
The <button> element looks to be dynamically generated so to click elements you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
driver.execute_script("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[data-testid$='republish-button'][name='republish']"))))
Using XPATH:
driver.execute_script("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[starts-with(#class, 'Button__ButtonContainer') and contains(#data-testid, 'republish-button')][#name='republish' and text()='Republish']"))))
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
I am trying to create an automated download event for one ERP platform using Selenium in python. This is a bit tricky for this part as there is no specific element to find and click.
Multiple buttons have the same class and no id for the highlighted button (DATABASE/BACKUP)
Can anyone help me with the same?
event flow >>
Login page > click "administration button" > click "database / backup"
thanks
As per Selenium documentation https://selenium-python.readthedocs.io/locating-elements.html you can find all of the elements by their class name and then access them by index ([index_number]).
all_buttons = driver.find_elements_by_class_name("your_class_name")
then you can do something like:
button_to_click = all_buttons[3] # The 4th button which has index 3 in all the retrieved buttons by class name
driver.find_element(By.XPATH,"//a[contains(text(),'Database / Backup')]").click()
Simply click the a tag which contains the text.
To click on the element with text as DATABASE/BACKUP you can use either of the following Locator Strategies:
Using xpath:
driver.find_element(By.XPATH, "//a[#class='menui' and contains(., 'DATABASE/BACKUP')]").click()
Using xpath:
driver.find_element(By.XPATH, "//a[text()[contains(., 'DATABASE/BACKUP')]]").click()
The desired element is a dynamic element, ideally to click on a clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='menui' and contains(., 'DATABASE/BACKUP')]"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[text()[contains(., 'DATABASE/BACKUP')]]"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
HTML snapshot:
Hi I want press the button labelled log in on this page, the html for the button is as follows:
<button class="">Log In</button>
the problem is there is multiple log in buttons on the same page with this code so I want to specify this button by using the id of the container the button is in:
<li id = 'bs-bk-PP" class = "_2t6uLu"
How do I find and click the log in button using these properties.
Thanks
To click on the element with text as Log In you can use either of the following Locator Strategies:
Using xpath:
driver.find_element(By.XPATH, "//li[#id='bs-bk-PP']//button[#class and text()='Log In']").click()
Ideally, to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//li[#id='bs-bk-PP']//button[#class and text()='Log In']"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
So I have this element in HTML...
<span style="font-size:11pt">Security</span>
... and I want to click it using Selenium in python. How can I do it? (Maybe I can identify it using onclick, but how?)
Also, here is what I've tried:
security = driver.find_element_by_xpath("//button[#onclick=\"on_catolog(2);\"]")
security.click()
You used wrong tag name in your XPath "//button[#onclick=\"on_catolog(2);\"]". It's not a button, but anchor tag. Try
"//a[#onclick='on_catolog(2);']"
Or click by link text:
security = driver.find_element_by_link_text("Security")
security.click()
To click on the element with text as Security you can use either of the following Locator Strategies:
Using css_selector:
driver.find_element_by_css_selector("a[onclick^='on_catolog'] > span[style='font-size:11pt']").click()
Using xpath:
driver.find_element_by_xpath("//a[starts-with(#onclick, 'on_catolog')]/span[text()='Security']").click()
Ideally, to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[onclick^='on_catolog'] > span[style='font-size:11pt']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[starts-with(#onclick, 'on_catolog')]/span[text()='Security']"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
I am trying to download an excel file using Selenium Python using the below code:
download=driver.find_element_by_css_selector("span[data-reactid='.0.1.0.0.1.1.1.0.1.0.1'>Download Orders]")
The element of the download button look like:
The piece of code above isnt working. Can someone help?
Your CSS locator is wrong. Please try to click with below xpath.
//span[.='Download Orders']
The desired element is a React element so to locate and click() on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using XPATH with normalize-space():
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[normalize-space()='Download Orders']"))).click()
Using XPATHand contains():
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[contains(., 'Download Orders')]"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
I agree with Muzzamil that the CSS locator you have used seems wrong. # is needed to select the attribute data-reactid.
You can try XPath:
//span[#data-reactid='.0.1.0.0.1.1.1.0.1.0.1']
If you want to include also the text, try:
//span[#data-reactid='.0.1.0.0.1.1.1.0.1.0.1'][contains(text(),'Download Orders')]