how to excract text from web site using selenium - python

i have Code like this:
<mat-label _ngcontent-vrc-c49="" class="filingCount ng-star-inserted">2 RRR</mat-label>
and i want value : 2 RRR
i tried with:
element = driver.find_element_by_xpath('//*[#id="maincontentid"]/app-dashboard/app-itr-status/div[4]/mat-label')
print(element.text)
but it's give the zero

Possibly you have to add wait until the element is visible. Like this:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 20)
element = wait.until(EC.visibility_of_element_located((By.XPATH, '//*[#id="maincontentid"]/app-dashboard/app-itr-status/div[4]/mat-label')))
print(element.text)
If still not working validate your locator correctness.

Related

Selenium Python find_element Xpath cant find xpath

I want to access an website with selenium and than a addblock-window appears, in which i need to click a button for it to disappear.
Eventhough I can find my XPath(//button[#title='Einverstanden'], /html/body/div/div[2]/div[3]/div[1]/button[#title = 'Einverstanden'] or
//button[contains(text(),"Einverstanden")]') in the browser,
I can't find it with my Python script. And i can't seem to find the mistake.
Here is my code.
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
driver = webdriver.Firefox()
driver.implicitly_wait(30)
driver.get("``https://www.derstandard.at/story/2000134260361/endspiel-vor-gericht-prozess-gegen-boris-becker-startet-in-london``")
driver.maximize_window()
x = driver.find_element(By.XPATH, "//button[#title = 'Einverstanden']")
print(x)
This is the error I'm getting.
The button is enclosed in an iframe in which case, you first need to switch to the iframe and then access the element
This should work:
driver.get("https://www.derstandard.at/consent/tcf/")
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//div[contains(#id, 'sp_message_container')]//iframe")))
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button[title='Einverstanden']"))).click()
Wait Imports:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
In case you want to switch to default frame again, you may use this when required:
driver.switch_to.default_content()

How to select a frame using selenium with target?

link = driver.find_element_by_partial_link_text('/wse/gupmenug.menu?p_sistema_c=ESCOLAR&p_sistemaid_n=1&p_menupredid_n=1&p_pidm_n=425370')
How can i fix this?
Your locator indeed looks invalid.
using the expected conditions and better locators it will be something like this:
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 20)
wait.until(EC.visibility_of_element_located((By.XPATH, "//a[text()='ALUMNOS']"))).click()
wait.until(EC.visibility_of_element_located((By.XPATH, "//a[text()='ESCOLAR']"))).click()

How to check whether the element text contains some text with python selenium?

Selenium: test if element contains some text
It is possible to do in selenium IDE but I don't know how to do it with python and selenium.
I want to set a waiting that wait until that element contains part of the specified text. Thanks.
You can wait for element like this :
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
element = wait.until(EC.visibility_of_element_located((By.ID, 'someid')))
extract the text like this :
actual_text = element.text
check whether it contains expected text or not like this:
self.assertIn('expected_string_here', actual_text)
or like this :
print expected_string_here == actual_text
Yes, you can use this:
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
WebDriverWait(self.driver, 30).until(
EC.text_to_be_present_in_element(By.ID, 'your_element_id', "The_text_you_are_looking_for"))
The element can be located by class name, css_selector, xpath etc.
The By.ID and the element locator should be updated accordingly

Python selenium can't locate element since it is not in the source code

I am trying to get the date that is highlighted in the calendar from https://web.archive.org/web/20110101000000*/area51.stackexchange.com
I can see the "calendar-day" class in chrome inspector but it is not displayed in the source code.
I have also tried to locate other class elements such as "month-week" but failed. Can anyone help me to diagnose what the problem is? I have looked into ShadowDOM but it seems not to be the issue here (I might be wrong though).
Furthermore, I am also trying to get the url "/web/20110430/area51.stackexchange.com" but don't know how to locate by class, tag name, css or Xpath.
driver = webdriver.Firefox()
driver.get("https://web.archive.org/web/20110101000000*/area51.stackexchange.com")
element=driver.find_element_by_class_name("calendar-day")
Thanks in advance!
Simply wait for the div element of calender to come up and print it. Your class name had an extra space as well as taking some time after a page loads.
element=WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "//div[contains(#class,'calendar-day')]")))
print(element.text)
Outputs 11
To grab multiple
elements=WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[contains(#class,'calendar-day')]")))
for element in elements:
print(element.text)
Outputs
11
30
7
16
18
10
11
7
12
Import
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
The page you are using is taking a while to load, so it will be better if you introduce some explicit wait before extracting.
A sample script can be:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox()
driver.get("https://web.archive.org/web/20110101000000*/area51.stackexchange.com")
wait = WebDriverWait(driver, 10)
element = wait.until(EC.presence_of_element_located((By.CLASS_NAME, "calendar-day ")))
print(element.text)
driver.quit()
To extract multiple element just need to add/change WebDriverWait(driver, 20).until(EC.presence_of_all_elements_located((By.CLASS_NAME, "calendar-day ")))
So updated sample script can be
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox()
driver.get("https://web.archive.org/web/20110101000000*/area51.stackexchange.com")
wait = WebDriverWait(driver, 10)
element = wait.(driver,20).until(EC.presence_of_all_elements_located((By.CLASS_NAME, "calendar-day ")))`
for el in element:
print(el.text)
driver.quit()

How to click the buttons A to Z using Selenium and Python within the webpage http://architects-register.org.uk/list/regions

http://architects-register.org.uk/list/regions
So i have this website which i want to be able to have the code click the button A, then B and C and so on. How should i do that. I tried this code which tries to get the value of the child of the element then click the buttons accordingly but it does not work. I suspect there's some other issues aside the error below.
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox()
url = 'http://architects-register.org.uk/'
driver.get(url)
find_uk = driver.find_element_by_id('ctl00_hlCountiesT').click()
elements = alphabets[0].find_element_by_tag_name('li')
def getChild(el):
return el.children[0].children[0]
for element in elements:
element = element[elements]
(getChild(element)).click()
NameError: name 'alphabets' is not defined
alphabets isn't being imported or defined anywhere in the given code block, so trying to access an index on it will fail in the way shown. It looks like alphabets should probably be the result of calling find_elements_by_tag_name or find_elements_by_class_name which would return a list of your buttons which you could then iterate over and click on.
Induce WebDriverWait and visibility_of_all_elements_located() and then click on each link.
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 import webdriver
driver = webdriver.Firefox()
url = 'http://architects-register.org.uk/'
driver.get(url)
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID,"ctl00_hlCountiesT"))).click()
elements =WebDriverWait(driver,10).until(EC.visibility_of_all_elements_located((By.XPATH,"//div[#class='alphabet_list_content']//li//input")))
for ele in range(len(elements)):
elements = WebDriverWait(driver,10).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[#class='alphabet_list_content']//li//input")))
elements[ele].click()
To click on the button A, then B and C and so on you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.get("http://architects-register.org.uk/list/regions")
for i in range(1, len(WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div.alphabet_list_content>ul li"))))):
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.alphabet_list_content>ul li:nth-of-type({})>a>input".format(i)))).click()
driver.quit()
Using XPATH:
driver.get("http://architects-register.org.uk/list/regions")
for i in range(1, len(WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[#class='alphabet_list_content']/ul//li"))))):
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='alphabet_list_content']/ul//following-sibling::li[{}]/a/input".format(i)))).click()
driver.quit()
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

Resources