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)
Related
I am currently writing a script for an internal website and feel like I am getting quite far with it. I have managed to webscrape the table I want with selenium and print it to the console. However now I am faced with the task of formatting the text into a format.
This is my code I need help with
Username = driver.find_elements(By.XPATH, value="/html/body/form[2]/table/tbody/tr/td[2]")
Role = driver.find_elements(By.XPATH, value="/html/body/form[2]/table/tbody/tr/td[3]")
Cluster_Access = driver.find_elements(By.XPATH, value="/html/body/form[2]/table/tbody/tr/td[4]")
Failed_Logins = driver.find_elements(By.XPATH, value="/html/body/form[2]/table/tbody/tr/td[5]")
Password_Exp = driver.find_elements(By.XPATH, value="/html/body/form[2]/table/tbody/tr/td[6]")
Mainframe_Data=[]
for i in range(len(Username)):
temporary_data={'Username': 'AU-Business-SSG-MF|S|TS347IAZ2|Company TECH|' + Username[i].text,
'Role': Role[i].text,
'Cluster Access' : Cluster_Access[i].text,
'Failed Logins' : Failed_Logins[i].text,
'Password Expiration' : Password_Exp[i].text}
Mainframe_Data.append(temporary_data)
for i in range(len(Username)):
temporary_data=['AU-NAB-SSG-MF|S|System1|IBM STORAGE|' + Username[i].text,
Role[i].text,
Cluster_Access[i].text,
Failed_Logins[i].text,
Password_Exp[i].text]
Mainframe_Data.append(temporary_data)
print(*Mainframe_Data,sep='\n')
I am wondering if a username equals a certain value I can change it and append it to Mainframe_Data.
This is the table Im scraping:
[1]: https://i.stack.imgur.com/B6cVQ.png
This is the format I need to make it output:
AU-Business-SSG-MF|S|System1|Company TECH|(User)||616/K/(Employee ID)|enabled|||Administrator
I somehow need to have an individual output for all systems on a separate line. For example in the picture provided I would need the following output:
AU-Business-SSG-MF|S|System1|Company TECH|(User)||616/K/(Employee ID)|enabled|||Administrator
AU-Business-SSG-MF|S|System2|Company TECH|(User)||616/K/(Employee ID)|enabled|||Administrator
AU-Business-SSG-MF|S|System3|Company TECH|(User)||616/K/(Employee ID)|enabled|||Administrator
AU-Business-SSG-MF|S|System4|Company TECH|(User)||616/K/(Employee ID)|enabled|||Administrator
I do realise not all the data is in the table I need but was hoping its possible to say if the username equals "User" I can fill it in with the format above.
I am hoping to do this as lightweight as possible
Im sorry if this question isn't really worded very well but I hope someone can help me
Thanks in advance
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 :)
as you can see in the first picture, my objective is to click and open every "Ver detalles" and to get all the text within it (which is shown in the third picture.
Here is the HTML for this first screen:
And here is the screen that opens once you click "ver detalles"
And its HTML
So far this I have made up some lines of code but I know it is useless because I am looking by XPATH and not by Class (and this will only return data for one), but whenever I look by class and try to iterate it doesn't find the class.
Please let me know if I made myself clear. Thanks beforehand
EDIT:
Thanks #cruisepandey it helped me open "ver detalles". Now I'm stucked trying to get the text out of it and click de X to close it and move on to the next "ver detalles".
This is the code I have so far, i have tried looking up by class, tag, etc but can't seem to find a way :(.
def order_data():
list_of_ver_detalles = driver.find_elements(By.XPATH, "//span[contains(text(),'Ver detalle')]/..")
sleep(3)
for ver in list_of_ver_detalles:
ver.click()
print(driver.find_element_by_class_name("jss672").text)
sleep(2)
driver.find_element(By.XPATH, "/html[1]/body[1]/div[6]/div[3]/div[1]/div[1]/div[1]/img[1]").click
Here is the text I am trying to print
And here is the X I am trying to click
I want to clarify few of your doubts when you say it is useless because I am looking by XPATH and not by Class - no it is not. for finding more than one element with any locator (assuming that locator in DOM can represent multiple entity) all you have to do is to use find_elements instead of find_element.
store all of Ver detalle like this :
list_of_ver_links = driver.find_elements(By.XPATH, "//span[contains(text(),'Ver detalle')]/..")
for ver in list_of_ver_links:
ver.click()
#Now write the code to fetch order details here
I'm trying to use selenium & python to auto get part of the html content.
I use find xpath like below ways to sort out the price from specified flight number, but always get failed result " unable to locate "
Anyone could shed some lights on it ?
element_price = driver.find_element_by_xpath("//div[#id='flight_MU5401']")
element_price.find_element_by_xpath(".//span[#class='base_price02']")
It's the html
If I were to guess, I would say that you are probably getting that error because the element isn't loaded when your code runs. It's probably a slower loading page. You should try adding a wait and see if that helps.
You can also simplify your locator by using a CSS selector and only scraping the page once.
price = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "#flight_MU5401 span.base_price02")).text
I'm trying to extract some text from a page using Python and Selenium The text is visible to me, but I can't work out how to extract it - I think the text was created in Java.
Im on the URL: "https://sellercentral.amazon.co.uk/hz/fba/profitabilitycalculator/index?lang=en_GB" and have entered the product id 'B00FRJ1R4M' for example, pressed search, then entered '20' in the Amazon Fulfilment Item Price box and pressed calculate.
I'm trying to extract the '-5.59' but to no avail.
The closest I think I've got is the follwing code:
cost = driver.find_element_by_xpath("//*[#id='afn-fees']/dl/dd[15]/input")
print(cost.get_attribute('innerHTML'))
print(driver.execute_script("return arguments[0].innerHTML", cost))
But this for returns 'None'.
Any help would be much appreciated.
You need to use .get_attribute("value"), since this is an input, and simplify your locator:
cost = driver.find_element_by_css_selector("input.cost-total")
print(cost.get_attribute("value"))
Here input.cost-total CSS selector would match an input element having cost-total class, which is quite readable and reliable locator in this case.