Python Selenium help finding xpath - python

Can you help me? I need to find the element.
I have tried this so far :
browser.find_elements_by_xpath('//input[#type="text"]/text()')
browser.find_elements_by_xpath('//input[type="text"]/text()')
browser.find_elements_by_xpath('/input[#type="text"]/text()')
browser.find_elements_by_xpath('/input[type="text"]/text()')
browser.find_elements_by_xpath('//input[#type="text"]')
None of them turned out working. ):

Try the following:
browser.find_element_by_xpath('//input[contains(#class,"ScInputBase")]').get_attribute("value")
I guess it's a value, not a text.
Also use find_element_by_xpath, not find_elements_by_xpath
I'm not sure about the uniqueness of the locator I used since I can't see your page.

Related

I'm having trouble selectiong an element using Selenium with Python

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?

I found a span on a website that is not visible and I can't scrape it! Why?

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)

Reference to XPath - is it possible?

I am using Selenium to automate a browser task through a Python script.
There is a text-box in my browser that I need to fill with info, but the XPath is formatted as below:
//*[#id="NameInputId14514271346457986"]
The problem is that: everytime the number before the Id (14514271346457986) changes. Is there a way to refer to this XPath something like:
//*[#id.start-with="NameInputId"]
Sorry if it is a dumb question - I started to using Selenium this week and I couldn't find this info on documentation.
You can test whether the first 9 characters of the #id value equals "NameInput", for an XPath 1.0 expression:
//*[fn:substring(#id, 1, 9) = "NameInput"]
With XPath 2.0 (and greater) you could use the starts-with() function:
//*[starts-with(#id, "NameInput")]
Sure, you can use xpath like //*[contains(#id,"NameInputId")] but I guess this possibly will not be an unique locator. In this case the xpath should be more complex to contain additional attributes or some parent element
you can use xpath like this
//*[#id[contains(.,'NameInputId')]]

How can I find the randomly Xpath using Selenium?

I am a newer in Selenium and use python to build it. Recently, I found a question which want to ask someone who can help me to figure it. The question is the Xpath I want to get is randomly, for example:
'//*[#id="wiki-edit-wikiEdit26"]/div/div/div/div[2]/a[1]'
'//*[#id="wiki-edit-wikiEdit27"]/div/div/div/div[2]/a[1]'
'//*[#id="wiki-edit-wikiEdit28"]/div/div/div/div[2]/a[1]'
These three xpath are used on the same button, but the number after wikiEdit will be changed every time. Therefore, are there any way which can help me to run my script more smoothly? Thank you very much!
Here is my python code:
broswer.find.element_by_xpath('//*[#id="wiki-edit-wikiEdit26"]/div/div/div/div[2]/a[1]') .click()
You can use matches in xpath to do this,
broswer.find.element_by_xpath("//*[matches(#id, '^(wiki-edit-wikiEdit)[0-9]')]/div/div/div/div[2]/a[1]") .click()
so basically that matches the id anything starting wiki-edit-wikiEdit followed by numbers form [0-9]
You just need to format your string..
import random
# Random range 1 - 100
x = random.randint(1,100)
broswer.find.element_by_xpath(f'//*[#id="wiki-edit-wikiEdit{x}"]/div/div/div/div[2]/a[1]') .click()
You can use starts-with or contains
broswer.find.element_by_xpath("//*[contains(#id,'wiki-edit-wikiEdit')]/div/div/div/div[2]/a[1]").click()
Also, using such long xpath's is not recommended. Use css-selectors over xpath.

Parsing a wiki-styled web page, XPath error

I am new to XPath, and I totally fail to parse a simple wiki-styled web page with lxml.
I have a following expression:
"".join(tree.xpath('//*[#id="mw-content-text"]/div[1]/p//text()'))
It works fine, but I need to exclude children whose class is "reference" and get a lxml.etree.XPathEvalError with a following expression:
"".join(tree.xpath('//*[#id="mw-content-text"]/div[1]/p//*[not(#class="reference")].text()'))
What is the right XPath expression? Thanks in advance :)
Probably, the error occured because of .text() instead of /text().
If you want include also text of p elements then you have to use the descendant-or-self XPath axis:
//*[#id="mw-content-text"]/div[1]/p/descendant-or-self::*[not(#class="reference")]/text()

Categories

Resources