How to get href from class using python? - python

I want to get link from href from an element. I tried find_elements_by_css_selector but can't reach out it. Does anyone know how to do it?
<a class="name" title="Download" data-i18n="[title]clickToDownload" data-src="some-link" href="link-to-retreive">
</a>

Call get_attribute on each of the links:
links = browser.find_elements_by_partial_link_text('##')
for link in links:
print(link.get_attribute("href"))
OR
lnks=driver.find_elements_by_tag_name("a")
# traverse list
for lnk in lnks:
# get_attribute() to get all href
print(lnk.get_attribute(href))
driver.quit()

To print the value of the href attribute you can use either of the following Locator Strategies:
Using css_selector:
print(driver.find_element_by_css_selector("a.name[title='Download']").get_attribute("href"))
Using xpath:
print(driver.find_element_by_xpath("//a[#class='name' and #title='Download']").get_attribute("href"))
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.name[title='Download']"))).get_attribute("value"))
Using XPATH:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[#class='name' and #title='Download']"))).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

Python Selenium: How can I print the link?

How can I print the value of the href attribute?
How can I print the link aaaaa.pdf with python selenium?
HTML:
<div class="xxxx">
</div>
You can do like this:
print(driver.find_element_by_css_selector(".xxxx a").get_attribute('href'))
div.xxxx a
first, check if this CSS_SELECTOR is representing the desired element.
Steps to check:
Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste the css and see, if your desired element is getting highlighted with 1/1 matching node.
If yes, then use explicit waits:
wait = WebDriverWait(driver, 20)
print(wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.xxxx a"))).get_attribute('href'))
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 the below:
pName = driver.find_element_by_css_selector(".xxxx").text
print(pName)
or
pName = driver.find_element_by_css_selector(".xxxx").get_attribute("href")
print(pName)
The value of the href attribute i.e. aaaaa.pdf is within the <a> tag which is the only descendant of the <div> tag.
Solution
To print the value of the href attribute you can use either of the following locator strategies:
Using css_selector:
print(driver.find_element(By.CSS_SELECTOR, "div.xxxx > a").get_attribute("href"))
Using xpath:
print(driver.find_element(By.XPATH, "//div[#class='xxxx']/a").get_attribute("href"))
To extract the value ideally you have 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, "div.xxxx > a"))).get_attribute("href"))
Using XPATH:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[#class='xxxx']/a"))).get_attribute("href"))
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 find an element by a known src attribute using Selenium Python

I am trying to find an element by using Selenium with python, by a known value of src attribute. The element is the point picture on the left of the address of a location in Google Maps.
Namely the html for the element I'm trying to select:
<img alt="" jstcache="935" src="//www.gstatic.com/images/icons/material/system_gm/1x/place_gm_blue_24dp.png" class="Liguzb" jsan="7.Liguzb,0.alt,8.src">
How can I select the given element by searching for it by using the link:
www.gstatic.com/images/icons/material/system_gm/1x/place_gm_blue_24dp.png
Thanks.
To locate the element as the value of src attribute is know to you, you can use either of the following Locator Strategies:
Using css_selector:
element = driver.find_element(By.CSS_SELECTOR, "img.Liguzb[src*='gstatic.com/images/icons/material/system_gm/1x/place_gm_blue_24dp']")
Using xpath:
element = driver.find_element(By.XPATH, "//img[#class='Liguzb' and contains(#src, 'gstatic.com/images/icons/material/system_gm/1x/place_gm_blue_24dp')]")
To locate a visible element instead of presence_of_element_located() you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "img.Liguzb[src*='gstatic.com/images/icons/material/system_gm/1x/place_gm_blue_24dp']")))
Using XPATH:
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//img[#class='Liguzb' and contains(#src, 'gstatic.com/images/icons/material/system_gm/1x/place_gm_blue_24dp')]")))
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 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

How to use css selector path for selenium to get an attribute of an span class - python

I would like to get the title attribute from the span class = g47SY lOXF2 but I can't find the correct css path.
Here is what I tried:
Number = self.browser.find_element_by_css_selector('ul li a span').get_attribute('title')
but it does not work.
Here is the HTML:
To print the value of the title attribute i.e 251 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[href$='followers/']>span[title]"))).get_attribute("title"))
Using XPATH:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[contains(#href, 'followers') and contains(., 'followers')]/span"))).get_attribute("title"))
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

XPATH is changing

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')]

Categories