I am trying to get the product's seller yet it will not get the text. I assume this is some weird thing since the text is also a link. Any help?
Python Code:
self.sold_by = driver.find_element_by_css_selector('#sellerProfileTriggerId').text
HTML Element:
SKUniverse
Try like this:
self.sold_by = driver.find_element_by_css_selector('#sellerProfileTriggerId')
text_element=self.sold_by.text
print(text_element)
Also, why aren't you using xpath or id selectors! Just asking :)
Related
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 am currently trying to click each button on a webpage with Selenium in Python, the class and text is always the same for each button but each button has different ids. The ids, however, are within "data-paramaters" in {} and I can't figure out how to get the correct syntax for the xpath.
Here is a snippet of the website for one of the buttons:
<span class="contains-icon-details gc-btn gc-btn--s" data-isneededpromise="false" data-parameters="{"partner":"gs", "realId": "8da1d6a9-44d1-4556-bc12-92699749a30a", "tnId": "102086182829", "type": "details"}">More Details</span>
It seems the realId and the tnId are unique, so I would need to find the buttons with either one of those.
This works:
driver.find_element_by_xpath("//span[#class='contains-icon-details gc-btn gc-btn--s']").click()
but of course only for the first button as the class is always the same.
I tried something like this:
driver.find_element_by_xpath("//*[contains(#tnId, '102086182829')]").click()
but I get
Unable to locate element: //*[contains(#tnId, '102086182829')]
so definitely not the correct syntax.
I tried to find a solution online, but with no luck so far. Can anybody point me into the right direction? Thanks in advance.
In case realId value or tnId value is unique your XPath can be
driver.find_element_by_xpath("//*[contains(#data-parameters, '8da1d6a9-44d1-4556-bc12-92699749a30a')]").click()
or
driver.find_element_by_xpath("//*[contains(#data-parameters, '102086182829)]").click()
you should filter by the "data-parameters" attribute.
Try
driver.find_element_by_xpath("//span[contains(#data-parameters, '102086182829')]").click()
This is a dirty implementation of what you need. It would be better to extract the data-parameters field, deserialize JSON and check for the needed field;
spans = driver.find_element_by_xpath("//span[#class='contains-icon-details gc-btn gc-btn--s']")
for span in spans:
data_parameters = span.get_attribute("data-parameters")
try:
data_parameters = json.loads(data_parameters)
except:
continue
if 'tnId' in data_parameters and data_parameters['tnId'] == "102086182829":
span.click()
break
please help me to find the solution to get the information from this html code by using Selenium without XPath because I want to make a loop from it.
I want to get the result as: "4.7" from this "title="4.7/5 - 10378 Reviews"
please check the picture below:
enter image description here
If you have driver setup already,
Than try something like this,
rating = driver.find_element_by_class_name("stars").get_attribute("title")
print (rating)
I have a website that I'm trying to automate but I can't find a particular link with selenium to click on. It looks like a link on the website, but when I use the chrome "inspect" function, it looks like it might be a button (???). I've tried copying the Xpath, but that doesn't work.
Here is the HTML behind the link
<button ng-bind-html="::ListingCtrl.copy.planListing.noPreference" track="No Preference" ng-click="::ListingCtrl.enterNoPreference()" class="link ally-focus-within">No Preference</button>
The Xpath for it is
//*[#id="mainContent"]/div/div/div/div[2]/ul/li[1]/h2/button
The text of the link is "No Preference", so also tried the following
elem_NoPreference = browser.find_element_by_xpath('//#track=\'No Preference\'')
But I'm not sure if my quote escape characters are correct.
I also unsuccessfully tried the following
elem_NoPreference = browser.find_element_by_link_text('No Preference') <br>
elem_NoPreference = browser.find_element_by_class_name('link ally-focus-within') <br>
elem_NoPreference = browser .find_element_by_css_selector("button[class='link ally-focus-within']
I should mention that the following are unique in the HTML. So, if there is a way to find these using the Xpath, it would be helpful
ListingCtrl.copy.planListing.noPreference
ListingCtrl.enterNoPreference()
track = "No Preference"
I'm at my wits end here. Any help would be appreciated.
Thank you!
If you don't plan to do any I18N / L10N testing you can stick to No Preference text, in this case the selector would be:
//button[text()='No Preference']
other option is basically the same, but instead of text it looks for track HTML attribute:
More information:
XPath Tutorial
XPath Operators & Functions
Xpath cheatsheet
Try with this xpath:
elem_NoPreference = browser.find_element_by_xpath("//button[#track='No Preference']")
or, if you want to select the element by the containing text:
elem_NoPreference = browser.find_element_by_xpath("//button[contains(.,'No Preference')]")
I am using the following code using Python 3.6 and selenium:
element = driver.find_element_by_class_name("first_result_price")
print(element)
on the website it is like this
`website: span class="first_result_price">712
however if I print element I get a completely different number?
Any suggestions?
many thanks!!
"element" is a type of object called WebElement that Selenium adds. If you want to find the text inside that element, you have to say
element.text
Which should return what you're looking for, '712', albeit in string form.