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
Related
How to click this button:
I have tried the following:
sumbitbutton = driver.find_element(By.XPATH, "//div[text() = 'mt8 mb8']")
I decided to insted abandon this method and go for a nuther as it lead to a lot of other problums latter down the line
You will want something like:
button = WebDriverWait(browser, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, "inputButton.main_submit")))
button.click()
As you did not confirm the URL, I cannot test it, but it should work.
Also, do not forget to import
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Generally <div> tags aren't clickable. Additionally mt8 mb8 are the classanmes, not the text.
Solution
As per the HTML:
to click on the <input> 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, "input.inputButton.main_submit[value='Submit']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='inputButton.main_submit' and #value='Submit']"))).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 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
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
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 have the below html mark-up I am trying to access and click via python... for some reason copying the xpath and doing this is not working:
self.driver.find_element(By.XPATH, '//*`[#id="isc_8D"]/table/tbody/tr/td/table/tbody/tr/td[2]/img')`
It seems the 'name' attribute is the only unique identifier below; how could I WAIT for it to exist first, then find element by name attribute and click in python? i.e. name="isc_NXicon"
<img src="http://website:8080/DBWEBSITE/ui/sc/skins/Enterprise/images/TabSet/close.png" width="12" height="12" align="absmiddle" style="vertical-align:middle" name="isc_NXicon" eventpart="icon" border="0" suppress="TRUE" draggable="true">
I am doing the below via different element with CSS selector: But How could I do the same via html 'name attribute' for my current relevant mark-up?
WebDriverWait(self.driver, 15).until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".btn.btn-mini.btn-primary"))).click()
To locate and click() on the desired element instead of using visibility_of_element_located() you need to use WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "img[name='isc_NXicon'][src$='DBWEBSITE/ui/sc/skins/Enterprise/images/TabSet/close.png']"))).click()
Using XPATH:
WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//img[#name='isc_NXicon' and contains(#src, 'DBWEBSITE/ui/sc/skins/Enterprise/images/TabSet/close.png')]"))).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