I need to find the XPath for the following element below:
<a> href="/resident/register/">Register another device </a>
I assumed the solution would be
$x("//*[contains(#href, 'resident/register')]")
But this has returned nothing. Any ideas?
Your HTML is malformed.
Change
<a> href="/resident/register/">Register another device </a>
to
Register another device
then your XPath will work as expected.
If your HTML is fixed, then you'll have to adjust your XPath to test the element content rather than the href attribute content:
//a[contains(.,'resident/register')]
but, although this can select the malformed a element, it won't be clickable since it lacks a proper href attribute.
To start with #kjhughes nailed one of the issue with the HTML i.e. HTML is malformed, as you must have tried to provide a correct HTML from your understanding.
The actual HTML I suppose is as follows:
Register another device
So an effective XPath for the element can be either of the following:
XPath:1
"//a[contains(#href, '/resident/register') and contains(.,'Register another device')]"
XPath:2
"//a[contains(#href, '/resident/register') and normalize-space()='Register another device']"
Related
I am trying to extract data from multiple pages of search results where the HTML in question looks like so:
<ul>
<li class="Card___StyledLi4-ulg8ho-7 jmevwM">...</li>
<li class="Card___StyledLi4-ulg8ho-7 jmevwM">...</li>
<li class="Card___StyledLi4-ulg8ho-7 jmevwM">...</li>
</ul>
I want to extract the text from the "li" tags, so I have:
text_data = WebDriverWait(driver,10).until(EC.visibility_of_all_element_located((By.XPATH,'Card___StyledLi4-ulg8ho-7.jmevwM')
print(text_data.text)
to wait and target "li" item. However, I get a "TimeoutException" error.
However, if I try to locate a single "li" item using the XPATH under the same conditions, the data is returned which leads me to question if I am inputting the class correctly?
Can anyone tell me what I'm doing wrong? Please let me know if there is any further information, you'd like me to provide.
I believe the XPath for these list items would be //li[#class="Card___StyledLi4-ulg8ho-7 jmevwM"] (or //*[#class="Card___StyledLi4-ulg8ho-7 jmevwM"] if you want all elements with that class rather than just li tags). You can take a look at this cheatsheet and this tutorial for further rules and examples of XPath.
You can also just use CSS Selectors like (By.CSS_SELECTOR, '.Card___StyledLi4-ulg8ho-7.jmevwM') in this case.
You have mentioned the wrong locator type, it should be CSS_SELECTOR and also put a dot '.' in front of element's property, because it is a 'class':
text_data = WebDriverWait(driver,10).until(EC.visibility_of_all_element_located((By.CSS_SELECTOR,'.Card___StyledLi4-ulg8ho-7.jmevwM')
I'm trying to get the URL link from an element with get_attribute('href') mode. However it returns null, like it didn't have href.
webdriver.find_element_by_xpath('//*[#id="__next"]/main/div[3]/div/section[1]/div/a[1]').get_attribute('href');
If I click manually, or use click() function, it will get me to the URL of the button, so there is a hyperlink associated to that button.
The html code of the element is that below:
<a data-testid="subcategory-content-with-no-link" class="styles__baseSubCategory-sc-rqlxha-0 styles__SubCategory-sc-rqlxha-3 kLvfti hFicjq">Mastite</a>
How can I get the URL/hyperlink of that button using Selenium?
Update
There's no href in any element inside <div> class as can be seen on this printscreen of html
.
Though on clicking the desired element you will be redirect to the desired URL but the href attribute is absent can be easily acknowledged from the value of the data-testid set as subcategory-content-with-no-link.
However, it needs to be emphasized that the href attribute is obfuscated within the class attribute value:
class="styles__baseSubCategory-sc-rqlxha-0 styles__SubCategory-sc-rqlxha-3 kLvfti hFicjq"
The html code that you posted shows that there is not href atteibute associated with that element. See if for example a parent element (possibly div) contains the href attribute instead.
Edit
As a workaround you can click the element to go to the url and get the url with driver.current_url
I am trying to get the href with selenium and python.
This is my page:
Some class information are changing depending on which elements. So I am trying basically to get all href for <a id="job____ .....
links.append(job.find_element_by_xpath('//a[#aria-live="polite"]//span').get_attribute(name="href"))
I tried couple of things but can't figure out how. How can i get all my href from the screenshot above?
Try this, but take care your xpath
"//a[#aria-live="polite"]//span"
will get a span, and i dont see any span with href on your html. Maybe this xpath solve it
//a[./span[#aria-live="polite"]]
links.append(job.find_element_by_xpath('//a[./span[#aria-live="polite"]]').get_attribute("href"))
But it wont get all urls, this with find_elements (return a list), extend your url list with list comprehension
links.extend([x.get_attribute("href") for x in job.find_elements_by_xpath('//a[./span[#aria-live="polite"]]')])
edit 1, other xpath solution
links.extend(["website_base_url"+x.get_attribute("href") for x in job.find_elements_by_xpath('//a[contains(#id, "job_")]')])
list_of_elements_with_href = wd.find_elements_by_xpath("//a[contains(#href,'')]")
for el_with_href in list_of_elements_with_href :
links.append(el.with_href.get_attribute("href"))
or if you need more specify:
list_of_elements_with_href = wd.find_elements_by_xpath("//a[contains(#href,'') and contains(#id,'job_')]")
Based on your description and attached image, I think you have got the wrong xpath. Try the following code.
find_links = driver.find_elements_by_xpath("//a[starts-with(#id,'job_')]")
links = []
for link in find_links:
links.append(link.get_attribute("href"))
Please note elements in find_elements_by_xpath instead of element.
I am unable to test this solution as you have not provided the website.
I'm trying to get text using Selenium WebDriver and here is my code. Please note that I don't want to use XPath, because in my case the ID gets changed on every relaunch of the web page.
My code:
driver.find_element_by_class_name("05uR6d").text
HTML:
<div class="O5uR6d">to fasten stuff</div>
Error:
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: An invalid or illegal selector was specified (Session info: chrome=88.0.4324.150)
Error is specific to the line of code I mentioned above.
How can I fix this?
Use this xpath:
driver.find_element_by_xpath("//div[contains(text(),'to fasten stuff')]")
Or this CSS:
driver.find_element_by_css_selector(".O5uR6d")
If both won't work, improve your question by adding more data of HTML you are looking at.
It can be done using multiple ways let me try to explain most of them.
Get element by class name.
this is the most easiest solution to get any element by class name you can simply do is:
driver.find_element_by_class_selector('foo');
Get Element by xpath
This is a bit tricky one, you can apply xpath either the class name, title, id or whatever remains same. it also works even if there's a text inside your div. For example:
driver.find_element_by_xpath("//tagname[#attribute='value']")
or in your case:
driver.find_element_by_xpath("//div['class='O5uR6d']")
or you can do something like #vitaliis said
driver.find_element_by_xpath("//div[contains(text(),'to fasten stuff')]")
You can read more about xpath and how to find it on this link
Get Elements by ID:
You can also get the element from id if there's any that's static:
driver.find_element_by_id('baz')
Get Elements by Name:
Get Elements by name using the following syntax:
driver.find_element_by_name('bazz')
Using CSS Selectors:
You can also use the css selectors to find the elements. Consider a following tag that has some attributes:
<p class="content">Site content goes here.</p>
You can get this element by:
driver.find_element_by_css_selector('p.content')
You can read more about it over here
Anyone know how to select this via xpath or css selector? The class name is used elsewhere in the HTML markup. Also this div isn't always present in each information block.
I need the following output in this div:
Price: $15.77 (13% off)
Here's the link if you need to see the source code in more detail: https://www.amazon.com/gp/goldbox/ref=nav_cs_gb
You can get required output as
driver.find_element_by_xpath('//div[normalize-space(span)="Price:"]').text