How to locate the element using XPath Selenium and Python - python

I need to get the number "3" from this HTML with python selenium
<div class="number">3</div>
This is the XPATH:
//*[#id="roulette-recent"]/div/div[1]/div[1]/div/div
I tried something like
number = navegador.find_element_by_xpath('//*[#id="rouletterecent"]/div/div[1]/div[1]/div/div').get_attribute('class')

If this xpath
//*[#id="rouletterecent"]/div/div[1]/div[1]/div/div
represent the node:
<div class="number">3</div>
and you want to extract the text from it, you should use either:
number = navegador.find_element_by_xpath('//*[#id="rouletterecent"]/div/div[1]/div[1]/div/div').get_attribute('innerText')
print(number)
or
number = navegador.find_element_by_xpath('//*[#id="rouletterecent"]/div/div[1]/div[1]/div/div').text
print(number)

I think you're looking for:
number = navegador.find_element_by_xpath('//*[#id="rouletterecent"]/div/div[1]/div[1]/div/div').text

To print the text 3 you can use either of the following Locator Strategies:
Using css_selector and get_attribute("innerHTML"):
print(navegador.find_element(By.CSS_SELECTOR, "#roulette-recent div.number").get_attribute("innerHTML"))
Using xpath and text attribute:
print(navegador.find_element(By.XPATH, "//*[#id="roulette-recent"]//div[#class='number' and text()]").text)
Ideally you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR and text attribute:
print(WebDriverWait(navegador, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "#roulette-recent div.number"))).text)
Using XPATH and get_attribute("innerHTML"):
print(WebDriverWait(navegador, 20).until(EC.visibility_of_element_located((By.XPATH, "//*[#id="roulette-recent"]//div[#class='number' and text()]"))).get_attribute("innerHTML"))
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 find a relevant discussion in How to retrieve the text of a WebElement using Selenium - Python
References
Link to useful documentation:
get_attribute() method Gets the given attribute or property of the element.
text attribute returns The text of the element.
Difference between text and innerHTML using Selenium

Related

Get Youtube video title using classname and text attribute using Selenium and Python

Hi I'm using Python Selenium Webdriver to get Youtube title but keep getting more info than I'd like.
The line is:
driver.find_element_by_class_name("style-scope ytd-video-primary-info-renderer").text
Is there any way to fix it and make it more efficient so that it displays only the title.
Here is the test script Im using:
from selenium import webdriver as wd
from time import sleep as zz
driver = wd.Firefox(executable_path=r'./geckodriver.exe')
driver.get('https://www.youtube.com/watch?v=wma0szfIafk')
zz(4)
test_atr = driver.find_element_by_class_name("style-scope ytd-video-primary-info-renderer").text
print(test_atr)
To print the title text OBI-WAN KENOBI Official Trailer (2022) Teaser you can use either of the following Locator Strategies:
Using css_selector and get_attribute("innerHTML"):
print(driver.find_element(By.CSS_SELECTOR, "h1.title.style-scope.ytd-video-primary-info-renderer > yt-formatted-string.style-scope.ytd-video-primary-info-renderer").get_attribute("innerHTML"))
Using xpath and text attribute:
print(driver.find_element(By.XPATH, "//h1[#class='title style-scope ytd-video-primary-info-renderer']/yt-formatted-string[#class='style-scope ytd-video-primary-info-renderer']").text)
Ideally you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR and text attribute:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "h1.title.style-scope.ytd-video-primary-info-renderer > yt-formatted-string.style-scope.ytd-video-primary-info-renderer"))).text)
Using XPATH and get_attribute("innerHTML"):
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h1[#class='title style-scope ytd-video-primary-info-renderer']/yt-formatted-string[#class='style-scope ytd-video-primary-info-renderer']"))).get_attribute("innerHTML"))
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
Console Output:
OBI-WAN KENOBI Official Trailer (2022) Teaser
You can find a relevant discussion in How to retrieve the text of a WebElement using Selenium - Python
References
Link to useful documentation:
get_attribute() method Gets the given attribute or property of the element.
text attribute returns The text of the element.
Difference between text and innerHTML using Selenium

Unable to print the text using Python Selenium?

So I want to print out "ou" from this HTML via XPath.
<div class="syllable">ou</div>
This is my code:
elem = driver.find_elements_by_xpath('*my xpath*')
for my_elem in elem:
print(my_elem.text)
I used .text in this code but it just prints out nothing when it runs. It should be printing ou but it just prints nothing at all. If someone could please tell me what I am doing wrong that would be great.
To print the text ou you can use either of the following Locator Strategies:
Using css_selector and get_attribute("innerHTML"):
print(driver.find_element_by_css_selector("div.syllable").get_attribute("innerHTML"))
Using xpath and text attribute:
print(driver.find_element_by_xpath("//div[#class='syllable']").text)
Ideally you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR and text attribute:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.syllable"))).text)
Using XPATH and get_attribute():
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[#class='syllable']"))).get_attribute("innerHTML"))
Console Output:
ou
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 find a relevant discussion in How to retrieve the text of a WebElement using Selenium - Python
References
Link to useful documentation:
get_attribute() method Gets the given attribute or property of the element.
text attribute returns The text of the element.
Difference between text and innerHTML using Selenium
if you are unable to print the text using .text
then use .get_attribute("innerHTML")
this will defiantly print out the text.
eg.
your code should look like this
elem = driver.find_elements_by_xpath('*my xpath*').get_attribute("innerHTML")
for my_elem in elem:
print(my_elem)

Xpath and CssSelector not extracting data using Selenium and Python

I am trying to pull the total listings at the top of the main section (in this case it currently says 72 Listings). I have tried both By.XPATH and By.CSS_SELECTOR with no luck.... Any idea why this isn't working?
driver.get('https://swappa.com/mobile/buy/apple-iphone-8/att')
n = WebDriverWait(driver, 0.01).until(EC.presence_of_element_located((By.CSS_SELECTOR, ".col-xs-9 pull-left"))).text
To print the text 72 Listings you can use either of the following Locator Strategies:
Using css_selector and get_attribute("innerHTML"):
print(driver.find_element_by_css_selector("section#section_billboard h2 small").get_attribute("innerHTML"))
Using xpath and text attribute:
print(driver.find_element_by_xpath("//section[#id='section_billboard']//h2//small").text)
Ideally you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR and text attribute:
driver.get('https://swappa.com/mobile/buy/apple-iphone-8/att')
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "section#section_billboard h2 small"))).text)
Using XPATH and get_attribute():
driver.get('https://swappa.com/mobile/buy/apple-iphone-8/att')
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//section[#id='section_billboard']//h2//small"))).get_attribute("innerHTML"))
Console Output:
value
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 find a relevant discussion in How to retrieve the text of a WebElement using Selenium - Python
References
Link to useful documentation:
get_attribute() method Gets the given attribute or property of the element.
text attribute returns The text of the element.
Difference between text and innerHTML using Selenium

Selenium wont return text

So I have this code which is referencing this html class:
<div class="text-right _2jRRJJvarKXJGP9oRP-Bv0 pbBzNArud8-t5yrNZi8Wg rankings-list-volume">2775.60</div>
Vol = driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div/main/div/div/div[2]/a[1]/div[8]').text
print(Vol)
If I run it without the .text it returns just the element name and such so I know it is finding the element but it won't return the text. The 2775.60 value within the HTML is what it should be returning but instead, it just prints nothing.
To print the text 2769.94 you can use either of the following Locator Strategies:
Using css_selector and get_attribute():
print(driver.find_element_by_css_selector("div.rankings-list-volume").get_attribute("innerHTML"))
Using xpath and text attribute:
print(driver.find_element_by_xpath("//div[contains(#class, 'rankings-list-volume')]").text)
Ideally, to print the text 2769.94 you have to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR and get_attribute():
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.rankings-list-volume"))).get_attribute("innerHTML"))
Using XPATH and text attribute:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[contains(#class, 'rankings-list-volume')]"))).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
You can find a relevant discussion in How to retrieve the text of a WebElement using Selenium - Python
Outro
Link to useful documentation:
get_attribute() method Gets the given attribute or property of the element.
text attribute returns The text of the element.
Difference between text and innerHTML using Selenium

Python + Selenium: I can't get print text from div

Python + Selenium: I can't get print text from this div:
<div id="modal-content-18" class="modal-content" data-role="content">
<div>
SignUp Failed. Please Try Again.
</div>
</div>
I tried this:
resp = browser.find_element_by_class_name("modal-content").text
print resp
But it does not work.
Please help me.
I personally prefer xpaths because of cases like these. They can tackle many complex cases as well. Try the following:
resp = browser.find_element_by_xpath('//div[#class="modal-content"]/div').text
print resp
In case the element isn't visible on the screen. The text method will be none. In that case you need the textContent attribute. Use the following then:
resp = browser.find_element_by_xpath('//div[#class="modal-content"]/div').get_attribute("textContent")
print resp
Let me know if it works for you. Also make sure there is only one modal-content on the page. In case there are more than one, your css_selector is insufficient to identify this element. To check this you can run the following.
l = len(browser.find_elements_by_xpath('//div[#class="modal-content"]/div'))
print l
if it returns a number greater than 1, then the modal-content class alone isn't enough and you will need to expand on your selection criteria.
Induce WebDriverWait and visibility_of_element_located() and following locator strategy.
Using CLASS_NAME:
print(WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.CLASS_NAME,"modal-content"))).text)
Using XPATH:
print(WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.XPATH,"//div[#class='modal-content' and #data-role='content']"))).text)
You need to import followings.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
EDITED
Check the textContent attribute value.
print(WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.CLASS_NAME,"modal-content"))).get_attribute("textContent"))
OR
print(WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.XPATH,"//div[#class='modal-content' and #data-role='content']"))).get_attribute("textContent"))
The desired text SignUp Failed. Please Try Again. is within the child <div> so you have to induce WebDriverWait for the desired 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.modal-content[id^='modal-content-'][data-role='content']>div"))).get_attribute("innerHTML"))
Using XPATH:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[#class='modal-content' and starts-with(#id, 'modal-content-')][#data-role='content']/div"))).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
You can find a relevant discussion in How to retrieve the title attribute through Selenium using Python?
Outro
As per the documentation:
get_attribute() method Gets the given attribute or property of the element.
text attribute returns The text of the element.
Difference between text and innerHTML using Selenium

Categories

Resources