Getting text from a <pre> object with Selenium - python

I'm trying to get the text inside of <pre> tag and I have tried with get_attribute('text'), get_attribute('value'), .text(), .value(), get_attribute("innerHTML") but I keep failing:
Snapshot:
This is the code that i'm using:
import unittest
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome("chromedriver.exe")
driver.get("###")
elem=driver.find_element(By.ID, "login_admin").click()
elem=driver.find_element(By.XPATH, "/html/body/div[15]/div[2]/form/div[1]/input").send_keys("###")
elem=driver.find_element(By.XPATH, "/html/body/div[15]/div[2]/form/div[2]/input").send_keys("###")
elem=driver.find_element(By.XPATH, "/html/body/div[15]/div[3]/div/button[1]").click()
elem=driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/nav/div/div[1]/button/span[3]').click()
time.sleep(2)
elem = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH, '/html/body/div[1]/div[1]/nav/div/div[2]/ul/li/a')))
time.sleep(2)
elem=driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/nav/div/div[2]/ul/li/a').click()
time.sleep(2)
elem=driver.find_element(By.XPATH, '/html/body/div[1]/div[3]/div/div/div/div/div/div/div[2]/div/div/div/div/div/div/div[2]/div/span[1]/input[2]').send_keys('###')
time.sleep(1)
elem=driver.find_element(By.XPATH, '/html/body/div[1]/div[3]/div/div/div/div/div/div/div[2]/div/div/div/div/div/div/div[2]/div/span[2]/button').click()
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[#class='output']/pre[text()]"))).get_attribute("innerHTML"))
And this is what it says when print:

To print the text within the <pre> tag you can use either of the following locator strategies:
Using css_selector and get_attribute("innerHTML"):
print(driver.find_element(By.CSS_SELECTOR, "div.output > pre").get_attribute("innerHTML"))
Using xpath and text attribute:
print(driver.find_element(By.XPATH, "//div[#class='output']/pre[contains(., 'ContactUri')]").text)
To extract the 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.output > pre"))).text)
Using XPATH and get_attribute("innerHTML"):
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[#class='output']/pre[contains(., 'ContactUri')]"))).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

Related

How to locate the link of the tag I want from a tags with the same class with Selenium

I want to go to the jobs section on LinkedIn with selenium, but it leads to the connections(networks) section with the same class name. how do i solve this problem?
My code;
jobs_sec = driver.find_element("xpath", "//a[#class='app-aware-link global-nav__primary-link']")
jobs_sec.click()
The tag reached when run;
The desired element is a dynamic element, so 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, "a.app-aware-link.global-nav__primary-link[href^='https://www.linkedin.com/jobs']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='app-aware-link global-nav__primary-link' and starts-with(#href, 'https://www.linkedin.com/jobs')]"))).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
Try:
jobs_sec = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".app-aware-link.global-nav__primary-link")))
jobs_sec.click()
If that doesn't work, try as an alternative:
jobs_sec = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#data-nnt-old-href='https://www.linkedin.com/jobs/?']")))
jobs_sec.click()
You will also need to import:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

Extract title but give me wrong output using selenium

import time
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
#chrome to stay open to see what's happening in the real word or make it comment to close
options.add_experimental_option("detach", True)
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()),options=options)
URL ='https://advpalata.vrn.ru/registers/reestr_lawyers/'
driver.get(URL)
title=driver.find_element("xpath", '//ul[#class="letter-filter"]//li[1]')
title.click()
page_links = [element.get_attribute('href') for element in driver.find_elements(By.XPATH, "//td[#class='name']//a")]
for link in page_links:
driver.get(link)
time.sleep(2)
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h3"))).text)
driver.close()
I want to extract the name but they extract the name in different format they will give me output like these page link is https://advpalata.vrn.ru/registers/reestr_lawyers/abdullaev_parviz_zairhan_ogly/
\xd0\x90\xd0\xb1\xd0\xb0\xd0\xba\xd1\x83\xd0\xbc\xd0\xbe\xd0\xb2
but I want output these:
Абдуллаев Парвиз Заирхан оглы
The WebElements are dynamically loaded. So you need to wait for the elements/texts to completely load before you attempt to extract them. Moreover you don't need to explicitly encode to utf-8 as by default Python uses utf-8 encoding.
Solution
To print the name ideally you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following locator strategies:
Using TAG_NAME:
#_*_coding: utf-8_*_
# driver.execute("get", {'url': 'https://advpalata.vrn.ru/registers/reestr_lawyers/abdullaev_parviz_zairhan_ogly/'})
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.TAG_NAME, "h3"))).text)
Using CSS_SELECTOR:
#_*_coding: utf-8_*_
# driver.execute("get", {'url': 'https://advpalata.vrn.ru/registers/reestr_lawyers/abdullaev_parviz_zairhan_ogly/'})
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "h3"))).text)
Using XPATH:
#_*_coding: utf-8_*_
# driver.execute("get", {'url': 'https://advpalata.vrn.ru/registers/reestr_lawyers/abdullaev_parviz_zairhan_ogly/'})
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h3"))).text)
Console Output:
Абдуллаев Парвиз Заирхан оглы
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 can I adjust this line of selenium code to get the status info of this item?

I have this line of code which I am trying to use to obtain the status of an item. Here is the line of code:
item_status = driver.findElement(By.className("status-info")).getText();
I'm not sure how I can adjust this to retrieve the text seen here:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options=Options()
driver=webdriver.Chrome(options=options)
#Directing to site
driver.get("https://www.amazon.co.uk/Nintendo-Switch-OLED-Model-Neon/dp/B098TNW7NM/ref=sr_1_3?keywords=Nintendo+Switch&qid=1651147043&sr=8-3");
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[2]/span/form/div[3]/span[1]/span/input"))).click()
When you are doing this
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[1]/header/div/div[1]/div[2]/div/form/div[3]/div/span/input"))).click()
it will click on search icon, now on the result page, this xpath //span[#class='a-size-base a-color-success a-text-bold'] is not present hence nothing is getting printed on the console you are likely to face TimedoutException.
However looking at the screenshot that you've shared, I would say to use this xpath
//div[#id='availability']//span[contains(text(),'In stock.')]
If you want to print the text and tag
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[#id='availability']//span[contains(text(),'In stock.')]"))).get_attribute("innerHTML"))
If only text you want:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[#id='availability']//span[contains(text(),'In stock.')]"))).get_attribute("innerText"))
driver.findElement(By.className("status-info")) is the Java syntax and getText() is a Java method. Possibly you need Python syntax and method.
Solution
To print the text In stock. 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, "span.a-size-base.a-color-success.a-text-bold"))).text)
Using XPATH:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[#class='a-size-base a-color-success a-text-bold']"))).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

Selenium: How to extract the CSS background-image value

HTML:
<div class="__section-image" style="background-image: url("/img/all/05_element/continentImg/1.png");"></div>
at this html part how can I locate the background-image?
To get the background image you need to use value_of_css_property(property_name) method and inducing WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
import re
my_property = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div[class='__section-image']"))).value_of_css_property("background-image")
print(re.split('[()]',my_property)[1])
Using XPATH:
import re
my_property = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//div[#class='__section-image']"))).value_of_css_property("background-image")
print(re.split('[()]',my_property)[1])
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 click on the webelement with in the highlighted script using Selenium and Python

I tried:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,"//*[#value='Sign Out']")))
but no luck.. please see image for the html script
The desired element is a JavaScript enabled element so to click on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR and click():
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "form[action*='Logoff']>li>input[value='Sign Out']"))).click()
Using XPATH and submit():
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//form[contains(#action, 'Logoff')]/li/input[#value='Sign Out']"))).submit()
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

Categories