I have an element:
<span class="a-color-price hlb-price a-inline-block a-text-bold">$399.98</span>
and I want to extract the price value in dollars which appears in the element’s text.
I have tried:
driver.get_element_by_xpath('//*[#id="nav-search"]/form/div[2]/div/input').get_attribute("value")
What about:
SpanVariable = driver.get_element_by_xpath('Put Xpath Here')
SpanVariableValue = SpanVariable.text
print SpanVariableValue # $399.98
You need to grab the text of the element you are looking at. Place the element you located in a variable, and then you can call selenium functions to it. In this case, .text grabs the text of your span element for you.
I'm not sure what you're doing wrong, it should be pretty simple. I use C# but based on what you've given I'd try this. And if this doesn't work, use a upper case 't' for the ending '.Text;' part of the call.
my_spanText = driver.get_element_by_xpath("//span[#class='a-color-price hlb-price a-inline-block a-text-bold']").text;
This one worked for me:
element=driver.find_element_by_xpath('//*[#id="hlb-subcart"]/div[1]/span/span[2]').text
Related
I want to read out the text in this html element using selenium with python. I just can't find a way to find or select it without using the text (i don't want that because its content changes)
<div font-size="14px" color="text" class="sc-gtsrHT jFEWVt">0.101 ONE</div>
Do you have an idea how i could select it? The conventional ways listed in the documentation seem to not work for me. To be honest i'm not very good with html what doesn't make things any easier.
Thank you in advance
Try this :
element = browser.find_element_by_class_name('sc-gtsrHT jFEWVt').text
Or use a loop if you have several elements :
elements = browser.find_elements_by_class_name('sc-gtsrHT jFEWVt')
for e in elements:
print(e.text)
print(browser.find_element_by_xpath("//*[#class='sc-gtsrHT jFEWVt']").text)
You could simply grab it by class name. It's 2 class names so it would be like so. by_class_name only uses one.
If the class name isn't dynamic otherwise you'd have to right click and copy the xpath or find a unique identiftier.
Find by XPath as long as font size and color attribute are consistent. Be like,
//div[#font-size='14px' and #color='text' and starts-with(#class,'sc-')]
I guess the class name is random?
Currently I'm trying to scrape data from a website. Therefore I'm using Selenium.
Everything is working as it should. Until I realised I have to scrape a tooltiptext.
I found already different threads on stackoverflow that are providing an answer. Anyway I did not manage to solve this issue so far.
After a few hours of frustration I realised the following:
This span has nothing to do with the tooltip I guess. Because the tooltip looks like this:
There is actually a span that I can't read. I try to read it like this:
bewertung = driver.find_elements_by_xpath('//span[#class="a-icon-alt"]')
for item in bewertung:
print(item.text)
So Selenium finds this element. But unfortunatly '.text' returns nothing. Why is it always empty ?
And what for is the span from the first screenshot ? Btw. it is not displayed at the Website as well.
Since you've mentioned Selenium finds this element, I would assume you must have print the len of bewertung list
something like
print(len(bewertung))
if this list has some element in it, you could probably use innerText
bewertung = driver.find_elements_by_xpath('//span[#class="a-icon-alt"]')
for item in bewertung:
print(item.get_attribute("innerText"))
Note that, you are using find_elements which won't throw any error instead if it does not find the element it will return an empty list.
so if you use find_element instead, it would throw the exact error.
Also, I think you've xpath for the span (Which does not appear in UI, sometime they don't appear until some actions are triggered.)
You can try to use this xpath instead:
//i[#data-hook='average-stars-rating-anywhere']//span[#data-hook='acr-average-stars-rating-text']
Something like this in code:
bewertung = driver.find_elements_by_xpath("//i[#data-hook='average-stars-rating-anywhere']//span[#data-hook='acr-average-stars-rating-text']")
for item in bewertung:
print(item.text)
I am trying to use Selenium to find the text between a span element that has no class, id, or anything I can think of to find it specifically.
Here is the html:
HTML from inspect element on Chrome
I have tried the following:
reqStr = driver.find_elements_by_xpath('span[data-bind*=text]')
for i in reqStr:
print(i.get_attribute('text'))
But that results in it printing just an empty list '[]'.
Any advice on how to find the text "CHEM1040, PHYS1130 - Must be completed prior to taking this course" is appreciated
You could get it using a CSS Selector like:
reqStr = driver.find_element_by_css_selector('span[data-bind="text: $data.DisplayText() + \' \' + $data.DisplayTextExtension()"]')
Thanks for the help guys. I actually managed to find it using:
reqStr = driver.find_elements_by_xpath("//span[contains(text(),'*')]")
print(reqStr)
for i in reqStr:
print(i.text)
I had this before but must have slightly messed something up because I was not getting the correct results. Thanks!
I'm trying to get the value from this HTML tag automatically:
<span data-testid="TemperatureValue" class="CurrentConditions--tempValue--1RYJJ">84°</span>
The temperature value in the middle changes automatically.
Also, should I be using the XPath or the full Xpath for my find_element_by_xpath() variable?
Try with this
degree = driver.find_element_by_xpath("//*[#data-testid='TemperatureValue']").text
print(degree)
Use
driver.find_element_by_css_selector('span[data-testid="TemperatureValue"]').text
Store this in a variable and print it
If I have some HTML:
<span class="select2-selection__rendered" id="select2-plotResults-container" role="textbox" aria-readonly="true" title="50">50</span>
And I want to find it using something like:
driver.find_element_by_xpath('//*[contains(text(), "50")]')
The problem is that there is 500 somewhere before on the webpage and it's picking up on that, is there way to search for a perfect match to 50?
Instead of contains, search for a specific text value:
driver.find_element_by_xpath('//*[text()="50"]')
And if you know it will be a span element, you can be a little more specific:
driver.find_element_by_xpath('//span[text()="50"]')
Note that your question asks how to find an element by its text value. If possible and would apply to your situation, you should look for a specific class or id, if known and consistent.
You can search for it by its absolute Xpath. For that, inspect the page and find the element. Then right-click it and copy its Xpath or full Xpath.
Otherwise you can use the id:
driver.find_element_by_id("select2-plotResults-container")
Here is more on locating elements.
use something like this
msg_box=driver.find_element_by_class_name('_3u328') and driver.find_element_by_xpath('//div[#data-tab = "{}"]'.format('1'))