How to extract text between ::before and ::after - python

I would like to extract the text between ::before and ::after into a string. How can I use a for loop to extract all the text in selenium Python?

The text i is in between the ::before and ::after pseudoelements. So to extract the text you can use either of the following Locator Strategies:
Using css_selector:
print(driver.find_element(By.CSS_SELECTOR, "div.kbkey.button.red").text)
Using xpath:
print(driver.find_element(By.XPATH, "//div[#class='kbkey button red']").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:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.kbkey.button.red"))).text)
Using XPATH:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[#class='kbkey button red']"))).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
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

::before and ::after are just a pseudo elements.
Here you can extract the text from the div element itself.
In case there are several divs with class kbkey button red you can do something like this:
buttons = driver.find_elements_by_css_selector("div.kbkey.button.red")
for button in buttons:
print(button.text)

Related

Python Selenium print text

There is such HTML code on the page, I need to get its text for check, or rather 740 using Selenium.
<button data-v-3630a784="" class="currency-btn mr10"><div data-v-3630a784="" class="progress"><div data-v-3630a784="" class="progress-bg anim" style="transform: scaleX(0.458333);"></div></div> <div data-v-3630a784="" class="num flex align-center icon-mana">740</div> <!----></button>
To print the text 740 you can use either of the following locator strategies:
Using css_selector and get_attribute("innerHTML"):
print(driver.find_element(By.CSS_SELECTOR, "button[class*='currency-btn'] div.num.flex.align-center.icon-mana").get_attribute("innerHTML"))
Using xpath and text attribute:
print(driver.find_element(By.XPATH, "//button[contains(#class, 'currency-btn')]//div[#class='num flex align-center icon-mana']").text)
To extract the text 740 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, "button[class*='currency-btn'] div.num.flex.align-center.icon-mana"))).text)
Using XPATH and get_attribute("innerHTML"):
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//button[contains(#class, 'currency-btn')]//div[#class='num flex align-center icon-mana']"))).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

How to locate the element using XPath Selenium and 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

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 - find article by h1 and p text

How to find article on website by h1 and p text like on image below?
I tried this, where I can found all articles and I don't know how to find this one with text in h1 and by text in p. And then I would like to click on this.
text = driver.find_elements_by_xpath("//article/div[contains(#class,'inner-article')]/h1")
To extract and print the text Beanie Custom First and Red 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:
Printing Beanie Custom First:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "article.inner-article h1 > a.name-link[href='/shop/asd']"))).text)
Printing Red:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "article.inner-article p > a.name-link[href='/shop/asd']"))).text)
Using XPATH and get_attribute():
Printing Beanie Custom First:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//article[#class='inner-article']//h1/a[#class='name-link' and #href='/shop/asd']"))).get_attribute("innerHTML"))
Printing Red:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//article[#class='inner-article']//p/a[#class='name-link' and #href='/shop/asd']"))).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
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
text = driver.find_elements_by_xpath("//article/div[contains(#class,'inner-article')][h1/a[contains(text(),"Beanie")]][p/a[contains(text(),"Red")]]")
you can use above xpath, which will check whehter the parent element article/div has child elements h1/a and p/a with texts Beanie and Red respectively
in w3chool html editor is inside iframe so switch to iframe in your seelnium tests before tryng to find the element

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

Categories

Resources