I have to get xxxxxxxx in this code using Selenium in python. How to get href or xxxxxxxxxxxxx and how to access target also ?
<div class="card-header bg-light card-collapse p-0" id="readHeading13">
<a class="btn btn-link btn-block text-dark d-flex justify-content-between align-items-center py-2" data-toggle="collapse" href="#readCollapse13" aria-expanded="false" aria-controls="readCollapse13">13. owner <span class="accordion-arrow">
<i class="fas fa-arrow-down small">
</i>
</span>
</a>
</div>
<div id="readCollapse13" class="collapse show" aria-labelledby="readHeading13"><div class="card-body p-3">
<form>
<div class="form-group">
<a href="/address/xxxxxxxxxxxxx" target="_parent">xxxxxxxxxxxxx</a> <i><span class="text-monospace text-secondary">address</span></i></div></form></div></div></div>
CodePudding user response:
To print the text value xxxxxxxxxxxxx
you can use either of the following Locator Strategies:
Using
css_selector
and text attribute:print(driver.find_element_by_css_selector("div.card-body > form a[href*='address']").text)
Using
xpath
andget_attribute()
:print(driver.find_element_by_xpath("//div[contains(@class, 'card-body')]/form//a[contains(@href, 'address')]").get_attribute("innerHTML"))
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
andget_attribute()
::print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.card-body > form a[href*='address']"))).get_attribute("innerHTML"))
Using
XPATH
and text attribute::print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[@class='name' and @title='Download']"))).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
CodePudding user response:
I guess the unique locator here will be as following:
//div[@class="form-group" and(contains(text(),"address"))]//a
So, to you can get the href
attribute and the text values as following:
el = driver.find-element_by_xpath('//div[@ and(contains(text(),'address'))]//a')
el_href = el..get_attribute("href")
el_text = el.text