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
Related
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
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 have this HTML:
And I want get this text "rataoriginal". (This text changes, I need this part of the code as text)
I've tried
xpath = "//span[#class='_5h6Y_ _3Whw5 selectable-text invisible-space copyable-text']"
auxa = driver.find_element_by_xpath(xpath).text
print(auxa)
But it prints the same as print("\n"). I don't want to use beaultifulsoup for a while.
This HTML is from 'https://web.whatsapp.com'
The WebElement is a dynamic element, so to print the values 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, "span.selectable-text.invisible-space.copyable-text[dir='auto']"))).text)
Using XPATH:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[contains(#class, '') and contains(#class, 'invisible-space')][contains(#class, '') and #dir='auto']"))).text)
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
References
You can find a relevant discussion in:
How to retrieve the text of a WebElement using Selenium - Python
//*[contains(text(),"rataoriginal")] please use this xpath