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")
Related
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
I am using Python 3 and Selenium(Chromedriver). I want to check for a element with this command.
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[3]/div/div[6]/div[2]/div[2]/div/form/div[2]/div[5]/div/div/div/div[2]/button"))).click()
The problem is that the XPATH is constantly changing between two paths:
/html/body/div[3]/div/div[6]/div[2]/div[2]/div/form/div[2]/div[5]/div/div/div/div[2]/button
/html/body/div[3]/div/div[3]/div[2]/div[2]/div/form/div[2]/div[5]/div/div/div/div[2]/button
I want to tell Python, that if the element is not found, it should search for the other XPATH.
If you know a method to find the element without the XPATH, i would also be happy with the solution.
It does not work if you search for the elemnt by its containing text, because the language from the sites changes if use a proxy.
This is the "Inspect Element" code of the button:
<button aria-label="Mobilnummer hinzufügen" class="bg-white css-1eajgu7 ex41m6f0 btn-secondary-dark " type="button">Hinzufügen</button>
To click on the element with text as Hinzufügen you can use either of the following Locator Strategies:
Using css_selector:
driver.find_element_by_css_selector("button.bg-white.btn-secondary-dark[aria-label='Mobilnummer hinzufügen']").click()
Using xpath:
driver.find_element_by_xpath("//button[#aria-label='Mobilnummer hinzufügen' and text()='Hinzufügen']").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, "button.bg-white.btn-secondary-dark[aria-label='Mobilnummer hinzufügen']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#aria-label='Mobilnummer hinzufügen' and text()='Hinzufügen']))).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 to use this XPath:
//div[contains(#class, 'mex-mobile-phone')]//button[contains(#class, 'btn-secondary-dark')]
I want to get an element by text, but it is number? I guess?
For example,
<a class="ui-state-default" href="#">11</a>
It contain a number so this code didnt work
driver.find_element_by_xpath('//*[#text="11"]')
How can I find this element?
You can access a web element(i.e div tag in this case) by its text content this way:
driver.find_elements_by_xpath("//*[contains(text(), '11')]")
driver.find_element_by_xpath('//*[contains(text,"11")]')
Please use above xpath to workout with this element
To locate the element by it's text you can use either of the following xpath based Locator Strategies:
Using xpath and text():
element = driver.find_element_by_xpath("//*[text()='11']")
Using xpath and contains():
element = driver.find_element_by_xpath("//*[contains(., '11')]")
Ideally, to locate the element you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:
Using XPATH:
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[#class='ui-state-default' and text()='11']")))
Using XPATH:
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[#class='ui-state-default' and contains(., '11')]")))
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
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
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