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
Related
I want to click on a popup window but after trying several ways, I obtain a "TimeoutException" error.
This is the code that I'm trying:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='modal-footer modal-footer-gallit-postula' and #id='modal_comentarios']"))).click()
And I attach an image of the html
For further information, the popup is very similar to the one appearing in the url below after clicking on the green button that says "Postular" (to get the exact popup it's necessary to be logged in). https://trabajo.gallito.com.uy/anuncio/vendedor-automotriz-qm995
The desired element is within a Modal Dialog Box so to click on the clickable 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, "div.modal-footer.modal-footer-gallit-postula > button.btn.btn-primary.btn-color-postula"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='btn btn-primary btn-color-postula' and text()='Aceptar']"))).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
You can use time.sleep() to handle this. For context:
from selenium import...
import time
driver.get(url)
butn = driver.find_element(by=By.XPATH, value='//*
[#id="postular"]').click()
time.sleep(5)
driver.find_element(by=By.XPATH, value='____').click()
-I don't have the credentials to login, but this is how i handle modal alerts
I am trying to use selenium click to open dropdown menu and then click again to download a file. Common error I get is that the "element is not attached to the page document" Have tried different codes but here's one:
#button = driver.find_element_by_xpath('//ul[#class="dropdown-menu-right dropdown-menu"]')
wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.ID, "options-dropdown")))
element.click()
HTML snapshot:
Generally <ul> nodes along with it's decendent <li> nodes gets expanded clicking on <span> nodes.
Solution
To click and open the drop-down-menu i.e. to expand the <li> nodes 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, "span.dropdown-toggle#options-dropdown"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[#class='dropdown-toggle' and #id='options-dropdown']"))).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 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
im trying to code a steambot in python that will post in steamgroup;Im having problem with last step,
i cant find the sumbit button
<button type="submit" class="btn_green_white_innerfade btn_medium" id="commentthread_General_34191408_submit">
<span>Post Discussion</span>
</button>
To identify the submit button with text as Post Discussion you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
element = driver.find_element_by_css_selector("button.btn_green_white_innerfade.btn_medium[id^='commentthread_General_'][id$='_submit']>span")
Using XPATH:
element = driver.find_element_by_xpath("//button[#class='btn_green_white_innerfade btn_medium' and starts-with(#id, 'commentthread_General_')]/span[text()='Post Discussion']")
However, as it is a submit button so moving ahead you will invoke click() on it, so you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following solutions:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn_green_white_innerfade.btn_medium[id^='commentthread_General_'][id$='_submit']>span"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='btn_green_white_innerfade btn_medium' and starts-with(#id, 'commentthread_General_')]/span[text()='Post Discussion']"))).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
Here is your button, works 100%. I checked it locally.
driver.find_element(By.XPATH, "//button[#type='submit']")
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.