GOAL: Print "Eden Ivy" onto my console.
I am following the documentation for Selenium Webdriver.
The following is the line of interest. Here is the screenshot:
HTML:
Eden Ivy
How exactly do I grab and print "Eden Ivy" ?
I tried:
name = driver.find_element_by_class_name('sc-1b6bgon')
print(name)
and
name = driver.find_element_by_class_name('sc-1b6bgon-7 cGUerq')
print(name)
But they don't seem to be working. What am I doing wrong?
Edit: I can't use the words "Eden Ivy" when grabbing, it has to be by element. So that I can use this function for other names.
It would be difficult to extract the innerHTML using the class attributes as they are dynamic in nature.
To print the innerText Eden Ivy attribute you can use either of the following Locator Strategies:
Using xpath and text attribute:
print(driver.find_element_by_xpath("//h2[text()]//following::div[1]/span/a").text)
Ideally you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:
Using XPATH and get_attribute("innerHTML"):
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h2[text()]//following::div[1]/span/a"))).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
Related
I am trying to extract some text from this page
In particular I want to extract the text between the tags. I am using Selenium and the following code but even though the object is recognized, the text is an empty string. Below is the code I am using:
testo = driver.find_element_by_xpath('/html/body/span/pre[1]').text
What do think think it could be the issue?
The text within <pre> tag is within an <iframe>
So to extract the desired text you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the desired element to be clickable.
You can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#mainFrame")))
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "span.dettaglio_atto_testo"))).get_attribute("innerHTML"))
Using XPATH:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#id='mainFrame']")))
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[#class='dettaglio_atto_testo']/pre"))).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
Firstly, you should switch to iframe. And then you can use .getText() method.
If it doesn't work you can try this: .getAttribute("innerText")
I am trying to read the webpage where after selecting the dropdown menu, the message is displayed. When the message appears it has class
<div class="alert alert-info border-0 rounded-0"> No update currently available </div>
I wrote the following code to read the text but always getting an exception
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
alert = WebDriverWait(driver, 5).until(EC.alert_is_present)
print(alert.text)
at the line with webdriver wait, I am getting the below error
__init__() takes 1 positional argument but 2 were given
My goal is to read the text in the alert class and validate it further. Any help will be appreciated.
Though the WebElement's class attribute contains the value alert still it isn't a JavaScript enabled Alert and you have to deal with it as a regular WebElement.
To print the text No update currently available you can use either of the following Locator Strategies:
Using css_selector and get_attribute("innerHTML"):
print(driver.find_element_by_css_selector("div.alert.alert-info").get_attribute("innerHTML"))
Using xpath and text attribute:
print(driver.find_element_by_xpath("//div[contains(#class, 'alert') and contains(#class, 'alert-info')]").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.alert.alert-info"))).text)
Using XPATH and get_attribute():
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[contains(#class, 'alert') and contains(#class, 'alert-info')]"))).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
I have some HTML that I am using selenium to scrape, I want to scrape the text inside the small tags. I cannot use XPath as for other examples, the XPath changes.
This is the HTML:
<h3 class="price">
$28.04
<small>ex</small><br> <small>$30.84 <small>inc</small></small></h3>
I know you can use price = driver.find_elements_by_class_name("price") and using price[1].text to get the text but I end up getting a selenium webdriver element:
<selenium.webdriver.remote.webelement.WebElement (session="a95cede569123a83f5b043cd5e138c7c", element="a3cabc71-e3cf-4faa-8281-875f9e47d6a4")>
Is there a way to scrape the 30.84 text?
The text 30.84 is within a text node. So to print the text you have to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:
Using XPATH and childNodes:
print(driver.execute_script('return arguments[0].firstChild.textContent;', WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h3[#class='price']//small[.//small[text()='inc']]")))).strip())
Using XPATH and splitlines():
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h3[#class='price']//small[.//small[text()='inc']]"))).get_attribute("innerHTML").splitlines()[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
Reference
You can find a detailed relevant discussion in:
How to retrieve partial text from a text node using Selenium and Python
How to print the partial text from an element using Selenium and Python
I'm trying to extract the 'email' using selenium. I want to get the value="raipiwro#squizzy.net" directly from the box. How can i do this ?
Website link: https://www.squizzy.de/
Helloww, you're trying to get the attribute value of an element, so we should do that:
driver.find_element("tag name", 'input').get_attribute('value')
First we get the element, then, get it's value attribute which is the email
To extract the email address using Selenium 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, "input[name='mail']"))).get_attribute("value"))
Using XPATH:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//input[#name='mail']"))).get_attribute("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
I'm trying to find the element and click for the button "Not Now". I've tried with with css_selector, xpath, but I"m unable at all to find the proper way.
HTML:
To locate and click() on the element with text as Not Now you can use the following Locator Strategy:
Using xpath:
driver.find_element_by_xpath("//button[text()='Not Now']").click()
However, the element looks dynamic to me so you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategy:
Using XPATH:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div//button[text()='Not Now']"))).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
Reference
You can find a couple of relevant discussions in:
What does contains(., 'some text') refers to within xpath used in Selenium
While fetching all links,Ignore logout link from the loop and continue navigation in selenium java
How does dot(.) in xpath to take multiple form in identifying an element and matching a text