How to click on an element by title with Selenium Python - python

I'm trying to check a checkbox with selenium and the only available info is the title.
Is a dynamic element, so I need to select by the title.
I tried all the methods without success (contains #title, etc).
Can someone please help? I'm beginning to code.
The xpath is:
/html/body/div[7]/div[2]/div/div[2]/div/div[2]/div/div/div[1]/form/div/span[1]/input
The CSS Selector is:
#selectCommon > span:nth-child(1) > input:nth-child(1)
The HTML looks like:
<input type="checkbox" title="AM - AMERICAS">

To click() on the element with title as AM - AMERICAS you can use either of the following locator strategies:
Using css_selector:
driver.find_element(By.CSS_SELECTOR, "input[title^='AM'][title$='AMERICAS']").click()
Using xpath:
driver.find_element(By.XPATH, "//input[starts-with(#title, 'AM') and contains(#title, 'AMERICAS')]").click()
Ideally 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, "input[title^='AM'][title$='AMERICAS']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[starts-with(#title, 'AM') and contains(#title, 'AMERICAS')]"))).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

How to click on the first matching element having a specific background-url using Selenium Python

HTML:
<div class="__section-image" style="background-image: url("/img/all/05_element/continentImg/1.png");"></div>
The website have a lot of this html. I want to click the first of them.
To click on the first matching element you can use either of the following locator strategies:
Using css_selector:
driver.find_elements(By.CSS_SELECTOR, "div[class='__section-image']")[0].click()
Using xpath:
driver.find_elements(By.XPATH, "//div[#class='__section-image']")[0].click()
Ideally to click on the first matching element you need to induce WebDriverWait for the visibility_of_all_elements_located() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div[class='__section-image']")))[0].click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[#class='__section-image']")))[0].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

How to click on dynamic hyperlink which has dynamic text using Selenium Python

I would like to click on the dynamic URL using selenium python from a web page. I have the following HTML where 123456 is dynamic text linked to dynamic URL. I am not able to use driver.find_element_by_link_text() as text also dynamic. Can someone please help me with this?
<td class="resultsColumn">123456</td>
Note: Both URL and Text also dynamic
To click() on the dynamic URL using Selenium and python you can use either of the following locator strategies:
Using css_selector:
driver.find_element(By.CSS_SELECTOR, "td.resultsColumn > a[href*='serviceID']").click()
Using xpath:
driver.find_element(By.XPATH, "//td[#class='resultsColumn']/a[contains(#href, 'serviceID')]").click()
Ideally 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, "td.resultsColumn > a[href*='serviceID']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//td[#class='resultsColumn']/a[contains(#href, 'serviceID')]"))).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

How to click an element using Selenium Python

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

How to click the element with href attribute using Python Selenium

The href is like this:
href="../s-reminderNotice.asp?fname=b%2D3c%2DpLessonBooking%2Easp%3Flimit%3Dpl"
I just want to click() this text link on a website. My code is:
driver.find_element_by_xpath('//a[#href="../s-reminderNotice.asp?fname=b%2D3c%2DpLessonBooking%2Easp%3Flimit%3Dpl"]')
To click() on the element with text as Booking without Fixed Instructor you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using LINK_TEXT:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Booking without Fixed Instructor"))).click()
Using PARTIAL_LINK_TEXT:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Booking without Fixed Instructor"))).click()
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.sidelinkbold[href*='LessonBooking'][onmouseover*='Practical Training Booking']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='sidelinkbold' and contains(#href,'LessonBooking')][contains(#onmouseover, 'Practical Training Booking')]"))).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

Trying to find submit button in elements

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']")

Categories

Resources