If I have the following HTML <a id="id" class="class" href="href">Element Text</a> how would I get "id" to be returned? My current procedure is:
print("Attribute: " + element.get_attribute('id'))
print("Property: " + element.get_property('id'))
print("Class: " + element.get_attribute('class'))
But all of those return empty strings. I am, however, able to get the text by using element.text
EDIT: Here's a more in depth explanation
I'm looking for an element but that element's ID varies. There is, however, an element linked to the element I want that I can find using it's xpath and comparing it's text to a specific text that I know beforehand. The ID of that element is something in the form of someID_XX. By taking the XX and appending it to another fixed string, I can then search for the element that I actually want. My issue is that once I get the second element (not the one I want directly, but the one that can lead me to the one I want) I can't seem to get it's ID attribute even though it seems to have one in the html. My question is, how do I get the id attribute?
For me is working with
element.get_attribute('id')
At the beginning didn't work because I selected incorrectly a hidden javascript element without id.
But if the error persist on your case, you can try this:
element.get_attribute('outerHTML')
get the DOM for the element, and then parse the html to extract the id
Related
I have the custom attribute called upgrade-test="secondary-pull mktg-data-content in the following code snippet:
<section class="dvd-pull tech-pull-- secondary-pull--anonymous tech-pull--digital secondary-pull--dvd-ping tech-pull--minimise" upgrade-test="secondary-pull mktg-data-content" data-js="primary-pull" style="--primary-direct-d_user-bottom-pos:-290px;">
I am able to identify my element successfully by doing the following:
element = driver.find_element(By.XPATH, "//section[contains(#upgrade-test, 'mktg-data-content')]")
This mktg-data-content gets changed every time a user goes to a different page for example it could be sales-data-content for the sales page etc.
What I am after is to find a way to retrieve this dynamic text of this custom attribute and pass it to my variable. Any help would really be appreciated. Thanks
You need to fetch the attribute value using element.get_attribute("upgrade-test") and then need to do string manipulation.
elementval = driver.find_element(By.XPATH, "//section[contains(#upgrade-test, 'secondary-pull')]").get_attribute("upgrade-test")
print(elementval.split(" ")[-1])
Note:- splitted with space,which returns zero based list, index -1 means the last value of the list,
since you have two elements in the list you can use this as well
print(elementval.split(" ")[1])
You need to find a unique locator for that element.
Without seeing that page we can only guess, so I can guess tech-pull--digital and secondary-pull--dvd-ping class names are making a unique combination.
If so you can use the following code:
attribute_val = driver.find_element(By.CSS_SELECTOR, "section.tech-pull--digital.secondary-pull--dvd-ping").get_attribute("upgrade-test")
print(attribute_val.split(" ")[-1])
The first line here locates the element and retrieves the desired attribute value while the second line isolates the first part of the desired attribute value as explained by KunduK
I'm still trying to make a bot with python and selenium. I'm trying to click on the element with the highest value. The highest value is stored in the element
variable. I tried this script, but selenium doesn't manage to locate it...
element = driver.find_element_by_xpath('//p[#value="' + value_to_locate + '"]')
driver.execute_script("arguments[0].scrollIntoView;", element)
element.click()
Here is the HTML :
<p data-v-859a1d26="" class="ml-2">632 Bells</p>
Do you have any idea?
From W3Schools:
The value attribute specifies the value of an element.
If you have multiple paragraphs <p> with different values, what you really need is getting all the paragraphs' values, sorting them, and clicking the max.
Get multiple elements using driver.find_elements_by_xpath (note the plural - elements) and then use Python to execute the sorting logic.
element.text will get you the text content of the element.
Maybe your XPath gets screwed up when you are trying to '//p[#value="' + value_to_locate + '"]' try something like "//p[#value='{}']".format(value_to_locate)
So I have this site and I'm trying to obtain the location and size of an element based on this xpath "//div[#class='titlu']"
How you can see that is visible and has nothing special.
Now the problem I've faced is that when I'm doing the search for xpath like this
e = self.driver.find_element_by_xpath(xpath) the location and size
of e are both 0
Also, for some reason, if I'm trying to get the text like this:
e.text is going to show me an empty string, and I need to get the actual text in this way e.get_attribute("textContain")
So do you have any idea how can I get the location and size of this element?
There are two elements matching this xpath. driver.find_element_by_xpath returns the first one while you are looking for the second one. Use the ancestor <div> with id attribute for unique xpath
"//div[#id='content-detalii']//div[#class='titlu']"
I have been struggling with this for a while now.
I have tried various was of finding the xpath for the following highlighted HTML
I am trying to grab the dollar value listed under the highlighted Strong tag.
Here is what my last attempt looks like below:
try:
price = browser.find_element_by_xpath(".//table[#role='presentation']")
price.find_element_by_xpath(".//tbody")
price.find_element_by_xpath(".//tr")
price.find_element_by_xpath(".//td[#align='right']")
price.find_element_by_xpath(".//strong")
print(price.get_attribute("text"))
except:
print("Unable to find element text")
I attempted to access the table and all nested elements but I am still unable to access the highlighted portion. Using .text and get_attribute('text') also does not work.
Is there another way of accessing the nested element?
Or maybe I am not using XPath as it properly should be.
I have also tried the below:
price = browser.find_element_by_xpath("/html/body/div[4]")
UPDATE:
Here is the Full Code of the Site.
The Site I am using here is www.concursolutions.com
I am attempting to automate booking a flight using selenium.
When you reach the end of the process of booking and receive the price I am unable to print out the price based on the HTML.
It may have something to do with the HTML being a java script that is executed as you proceed.
Looking at the structure of the html, you could use this xpath expression:
//div[#id="gdsfarequote"]/center/table/tbody/tr[14]/td[2]/strong
Making it work
There are a few things keeping your code from working.
price.find_element_by_xpath(...) returns a new element.
Each time, you're not saving it to use with your next query. Thus, when you finally ask it for its text, you're still asking the <table> element—not the <strong> element.
Instead, you'll need to save each found element in order to use it as the scope for the next query:
table = browser.find_element_by_xpath(".//table[#role='presentation']")
tbody = table.find_element_by_xpath(".//tbody")
tr = tbody.find_element_by_xpath(".//tr")
td = tr.find_element_by_xpath(".//td[#align='right']")
strong = td.find_element_by_xpath(".//strong")
find_element_by_* returns the first matching element.
This means your call to tbody.find_element_by_xpath(".//tr") will return the first <tr> element in the <tbody>.
Instead, it looks like you want the third:
tr = tbody.find_element_by_xpath(".//tr[3]")
Note: XPath is 1-indexed.
get_attribute(...) returns HTML element attributes.
Therefore, get_attribute("text") will return the value of the text attribute on the element.
To return the text content of the element, use element.text:
strong.text
Cleaning it up
But even with the code working, there’s more that can be done to improve it.
You often don't need to specify every intermediate element.
Unless there is some ambiguity that needs to be resolved, you can ignore the <tbody> and <td> elements entirely:
table = browser.find_element_by_xpath(".//table[#role='presentation']")
tr = table.find_element_by_xpath(".//tr[3]")
strong = tr.find_element_by_xpath(".//strong")
XPath can be overkill.
If you're just looking for an element by its tag name, you can avoid XPath entirely:
strong = tr.find_element_by_tag_name("strong")
The fare row may change.
Instead of relying on a specific position, you can scope using a text search:
tr = table.find_element_by_xpath(".//tr[contains(text(), 'Base Fare')]")
Other <table> elements may be added to the page.
If the table had some header text, you could use the same text search approach as with the <tr>.
In this case, it would probably be more meaningful to scope to the #gdsfarequite <div> rather than something as ambiguous as a <table>:
farequote = browser.find_element_by_id("gdsfarequote")
tr = farequote.find_element_by_xpath(".//tr[contains(text(), 'Base Fare')]")
But even better, capybara-py provides a nice wrapper on top of Selenium, helping to make this even simpler and clearer:
fare_quote = page.find("#gdsfarequote")
base_fare_row = fare_quote.find("tr", text="Base Fare"):
base_fare = tr.find("strong").text
I have multiple questions inside which there are more than 1 options.
After selecting the required question element as question_element
I am unable to get the first text box inside this element. I used
question_element.find_elements_by_xpath("//textarea")
but it gives me list of all the elements with tag textarea in the whole webpage. I tried
question_element.find_elements_by_xpath("/textarea")
question_element.find_elements_by_xpath("./textarea")
but they didn't give any results. How do I get the first element with tag name textarea inside the question_element
There are two variants that work for search within already found element (not within the whole page):
question_element.find_elements_by_xpath(".//textarea")
Try like this
question_element.find_elements_by_xpath("//textarea[position()=1]")