How to extract the Email-Address using Selenium and Python - 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

Related

Selenium cannot extract text

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")

How to get the value of title from span using Python and Selenium

I have this code
followers_button = browser.find_element_by_xpath('/html/body/div[1]/section/main/div/header/section/ul/li[2]/a/span')
I need to get value of title from span. How can i do that?
To print the value of the title attribute you can use either of the following Locator Strategies:
Using css_selector:
print(driver.find_element(By.CSS_SELECTOR, "a[class*='na13'][href='/top_ukraine_girls/followers/']>span").get_attribute("title"))
Using xpath:
print(driver.find_element(By.XPATH, "//a[#class='-na13' and #href='/top_ukraine_girls/followers/']/span").get_attribute("title"))
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, "a[class*='na13'][href='/top_ukraine_girls/followers/']>span"))).text)
Using XPATH:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[#class='-na13' and #href='/top_ukraine_girls/followers/']/span"))).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
print(followers_button.get_attribute('title'))
I am assuming you want to get the title attribute value using get_attribute and not the text.
Outputs:
114 555
In case /html/body/div[1]/section/main/div/header/section/ul/li[2]/a/span is a corrct XPath locator
followers_button_text = browser.find_element_by_xpath('/html/body/div[1]/section/main/div/header/section/ul/li[2]/a/span').text
print(followers_button_text)
Should work

Selenium Webdriver: How do I grab by class name?

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

Selenium not able to click on Get Data button on using Python

I am scraping data from this website . The element is below and geckodriver
<img class="getdata-button" style="float:right;" src="/common/images/btn-get-data.gif" id="get" onclick="document.getElementById('submitMe').click()">
but can't get selenium to click it tried even xpath, id but not luck
is there any fix or work around to get it done?
To click on the element Get Data you can use either of the following Locator Strategies:
Using css_selector:
driver.find_element_by_css_selector("img.getdata-button#get").click()
Using xpath:
driver.find_element_by_xpath("//img[#class='getdata-button' and #id='get']").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, "img.getdata-button#get"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//img[#class='getdata-button' and #id='get']"))).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
You should probably try by id
driver.find_element(By.ID, 'get').click()

How can I click a href button using python 3.8 selenium?

The problem:
I'm trying to click this href here:
Fail attempts:
I tried to use these to no avail
driver.find_element_by_link_text('Join').click()
driver.find_element_by_partial_link_text('href').click()
You can use xpath instead of link text.
driver.find_element_by_xpath('//a[contains(text(), "John"]').click()
Or add space in front of John.
driver.find_element_by_link_text(' Join').click()
To click on the element with text as Join you can use either of the following Locator Strategies:
Using partial_link_text:
driver.find_element_by_partial_link_text("Join").click()
Using xpath:
driver.find_element_by_xpath("//a[contains(., 'Join')]").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 PARTIAL_LINK_TEXT:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Join"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(., 'Join')]"))).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

Categories