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
Related
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()
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 locate value inside a div using xpath \ css_selector
I have this html:
<div class="catalog-products view-tile" data-catalog-products="" data-slider-available="" data-primary-as-icon=""><div data-id="product" class="catalog-product ui-button-widget" data-product="5a7b4c6d-0bc3-11ec-a2b0-00155dfc8232" data-code="4862447" data-preview-slider-inited="1"><div class="catalog-product__image"><a class="catalog-product__image-link" href="/product/5a7b4c6d0bc3c823/videokarta-msi-geforce-210-n210-1gd3lp/" data-toggle-slider=""><picture><source type="image/webp" media="(min-width: 768px)" etc
So I need to get data-code value 4862447
Tried to access via xpath:
/html/body/div[1]/div/div[2]/div[2]/div[3]/div/div[1]/div[1]
Got highlighted in Chrome console that part:
<div data-id="product" class="catalog-product ui-button-widget" data-product="5a7b4c6d-0bc3-11ec-a2b0-00155dfc8232" data-code="4862447" data-preview-slider-inited="1">
Don't know how to get data-code value.
Tried css_selector:
div[data-id='product']
Got same line:
<div data-id="product" class="catalog-product ui-button-widget" data-product="5a7b4c6d-0bc3-11ec-a2b0-00155dfc8232" data-code="4862447" data-preview-slider-inited="1">
And no idea again.
To print the value of the data-code attribute you have to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.catalog-products.view-tile > div.catalog-product.ui-button-widget[data-id='product']"))).get_attribute("data-code"))
Using XPATH:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[#class='catalog-products view-tile']/div[#data-id='product' and #class='catalog-product ui-button-widget'][.//a[#class='catalog-product__image-link' and #href and #data-toggle-slider]]"))).get_attribute("data-code"))
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
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