HTML:
<button type="submit" class="button primary g-recaptcha"
data-sitekey="6LdSmG4cAAAAAAOarRxGIhehvv4sPgVeF-vRi-Jqb"
data-callback="onSubmit">Avanti</button>
Attempt 1:
WebDriverWait(driver, 20)
.until(EC.element_to_be_clickable((By.XPATH,
"/html/body/main/div/section[1]/form/button")))
.click()
Attempt 2:
WebDriverWait(driver, 20)
.until(EC.element_to_be_clickable((By.XPATH,
"//[#class='button primary g-recaptcha' and #type='submit']")))
.click()
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, "button.button.primary.g-recaptcha[data-callback='onSubmit']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='button primary g-recaptcha' and text()='Avanti']"))).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
Presumably, it's a recaptcha element and generally they resides within iframes. So you need to switch_to.frame() first as follows:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(By.ID,"id_of_iframe"))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.button.primary.g-recaptcha[data-callback='onSubmit']"))).click()
Related
I am looking for a way to click on the svg cross to close overlaying welcome window. I managed to go through login and authorization but this cross is getting me crazy.
Code trials:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, "jss109"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//svg[#class='jss109']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'svg[aria-hidden="True"]')).click()
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CLASS_NAME, "JSS109"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[#class='jss109']/*[name()='svg']")))
Error massage: TimeoutException: Message:
Snapshot of the HTML:
Another snapshot of the HTML:
To click on the X icon 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, "h2 svg.jss109[viewBox]"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//h2[.//span[starts-with(., 'Welcome to new')]]//*[name()='svg' and #class][#viewBox]"))).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:
<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
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
This is the HTML:
<li class="Y8-fY "><a class="-nal3 " href="/example/" tabindex="0"><span class="g47SY ">1.692</span> abonniert</a></li>
I tried using XPATH with the href to find it but i just get the nosuchelement error.
To click() on the desired element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using PARTIAL_LINK_TEXT:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "abonniert"))).click()
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "li > a[href='/example/'] > span"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//li/a[#href='/example/']/span"))).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
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