Using selenium to get access class info on website - python

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.

Related

xpath to check only within WebElement in Selenium / Python

I am very new to this and i have tried to look for the answer to this but unable to find any.
I am using Selenium+chromedriver, trying to monitor some items I am interested in.
Example:
a page with 20 items in a list.
Code:
#list of items on the page
search_area = driver.find_elements_by_xpath("//li[#data-testid='test']")
search_area[19].find_element_by_xpath("//p[#class='sc-hKwDye name']").text
this returns the name of item[0]
search_area[19].find_element_by_css_selector('.name').text
this returns the name of item[19]
why is xpath looking at the parent html?
I want xpath to return the name of item within the WebElement /list item. is it possible?
found the answer, add a . in front
hope this is gonna help someone new like me in the future.
from
search_area[19].find_element_by_xpath("//p[#class='sc-hKwDye name']").text
to
search_area[19].find_element_by_xpath(".//p[#class='sc-hKwDye name']").text
What you are passing in find_element_by_xpath("//p[#class='sc-hKwDye name']") is relative Xpath. You can pass the full Xpath to get the desired result.

Accessing data from class selenium

Currently trying to get the data which is stored in this class using selenium and print it into console.
<h3 class="tiktok-dvof16-AuthorTitle e10yw27c0">allawi_9</h3>
I tried print(driver.find_elements_by_class_name("tiktok-dvof16-AuthorTitle e10yw27c0"))
I also tried adding .text at the end, however you cant do that on a list obj. (And the returned list is empty may I add.
However, it just returns an empty list. I'm unsure what I'm doing wrong. Any help would be great!
if the element value is unique on the page you can use
myelemnt = driver.find_element_by_name('Some text')
myelement.print()
if not you can use X-path for this like
myelement = driver.find_element_by_xpath('/html/body/div[1]/section/main/div/div[3]/article/div[1]/div/div[1]/div[1]/a/div/div[2]')
myelement.print()
if that doesnt worked use Full x-path
myelement = driver.find_element_by_xpath('//*[#id="react-root"]/section/main/div/div[2]/article/div[1]/div/div[1]/div[1]/a/div/div[2]')
myelement.print()
for getting the x-path/full x-path simple just right-click on the element on the google chrome developer tools and click on Copy then x-path / full x-path

How to find an Element by index in selenium Webdriver for python

This is HTML code of that page
From there I want to access the 2nd element by using class name "maxbutton-1" as it has 3 same buttons and I can't use xpath or any constant selector so want to use the indexing with class and can't find anything to do that in python particular.
Also tried the method used in java to do same thing but it didn't worked.
Link of that same page
just trying to automate the movie downloading process for any movie.
Thank you.
To click on first, second or third button, try to change number of element:
el1 = driver.find_element_by_xpath("(//a[#class='maxbutton-1 maxbutton maxbutton-download-links'])[1]")
el2 = driver.find_element_by_xpath("(//a[#class='maxbutton-1 maxbutton maxbutton-download-links'])[2]")
el3 = driver.find_element_by_xpath("(//a[#class='maxbutton-1 maxbutton maxbutton-download-links'])[3]")
then you can extract element href/link attribute like that:
link = el.get_attribute('href')
or click it like that:
el.click()

How to parse Selenium driver elements?

I'm new in Selenium with Python. I'm trying to scrape some data but I can't figure out how to parse outputs from commands like this:
driver.find_elements_by_css_selector("div.flightbox")
I was trying to google some tutorial but I've found nothing for Python.
Could you give me a hint?
find_elements_by_css_selector() would return you a list of WebElement instances. Each web element has a number of methods and attributes available. For example, to get an inner text of the element, use .text:
for element in driver.find_elements_by_css_selector("div.flightbox"):
print(element.text)
You can also make a context-specific search to find other elements inside the current element. Taking into account, that I know what site you are working with, here is an example code to get the departure and arrival times for the first-way flight in a result box:
for result in driver.find_elements_by_css_selector("div.flightbox"):
departure_time = result.find_element_by_css_selector("div.departure p.p05 strong").text
arrival_time = result.find_element_by_css_selector("div.arrival p.p05 strong").text
print [departure_time, arrival_time]
Make sure you study Getting Started, Navigating and Locating Elements documentation pages.

Can't get the value of an attribute with Selenium RC using xpath

I'm trying to get the first href attribute in a page using Selenium RC (in Python):
sel.get_text("xpath=//#href")
this returns an empty string.
However, an identical xpath on the same page inside Firefox (using the "View XPath" extension) yields the correct value.
I've tried fiddling with it, but the same happens for other attributes (eg #class) -- is there something awfully wrong with selenium or am I overlooking something trivial here?
Solved by using selenium's get_attribute e.g. sel.get_attribute("xpath=//a#href") for a nodes.
In Selenium RC, you can use the get_attribute function as below.
AttrValue = sel.get_attribute("//li[#id='result_0']/div/div[3]/div/a#href")
where //li[#id='result_0']/div/div[3]/div/a is the xpath.
xpath= is not required inside the function.
I think this applies to all types of elements:
For example: for an input type of element
selenium.getAttribute("//input#value");

Categories