Write relative xpath that selects all values present in list - python

I am using website https://www.techlistic.com/p/demo-selenium-practice.html
wherein i am using Demo table 2 for the reference
I am trying to write an xpath for a Web table wherein I need to get the list of all details in structure column below in the below list format
Burj Khalifa
Clock Tower Hotel
Taipei 101
Financial Center
I have written an relative xpath = //th[normalize-space()='Burj Khalifa'] but it only selects the first value. I require an relative xpath wherein it selects all the above values present in the list so that I can run a for loop

The below xpath expression will select all of your above mentioned/required data:
//*[#class="tsc_table_s13"]/tbody/tr/th[1]

select table with class "tsc_table_s13" and find the required data
//table[#class="tsc_table_s13"]/tbody/tr/th
if you want to get all the data in the table then use
//table[#class="tsc_table_s13"]/tbody/tr to loop over every row and use relative paths to get the required data like .//th/text() and so on

Related

How to get a value from a web table using Python Selenium?

I can access to the page. I can go to the location of the table using Selenium xpath.
driver.find_elements('xpath', "/html/body/div/div/div[1]/div[2]/div[3]/div/div[4]/div[2]/div/div[2]/div/table/tbody/tr[2]/td[3]")
The question is how I can get the value inside the cell?
You need to assign the result of your expression to a variable which will be an array of element objects. Since you are directly referencing a td element, the array will only have one element, which should be the value you are looking for. To get this value, use element[0].text.
element = driver.find_elements('xpath', "/html/body/div/div/div[1]/div[2]/div[3]/div/div[4]/div[2]/div/div[2]/div/table/tbody/tr[2]/td[3]")
print(element[0].text)
Here is a simple example of it working with a generic HTML table from w3schools:
...
driver.get("https://www.w3schools.com/html/html_tables.asp")
element = driver.find_elements('xpath', "/html/body/div[7]/div[1]/div[1]/div[3]/div/table/tbody/tr[3]/td[1]")
print(element[0].text)
Output:
Centro comercial Moctezuma

Choose XPATH based on <th> string value with selenium

There is a table that I want to get the XPATH of, however the amount of rows and columns is inconsistent across results so I can't just right click and get copy the full XPATH.
My current code:
result_priority_number = driver.find_element(By.XPATH, "/html/body/div/div[2]/div[6]/div/div[2]/table/tbody/tr[18]/td[2]")
The table header names though are always consistent. How do I get the value of an element where the table header specifically says something (i.e. "Priority Number")
I can't just right click and get copy the full XPATH.
Never use this method. Xpath has a very useful feature for search! It isn't just for nested pathing!
//td[contains(text(),'header value')]
or if it has many tables and you want only one of its:
//table[#id='id_of_table']//td[contains(text(),'header value')]
or the table hasn't id or class:
//table[2]//td[contains(text(),'header value')]
where 2 is index of table in page
and other many feature for searching in html nodes
in your case, for get Filing language:
//td[contains(text(),'Filing language')]/following-sibling::td

accessing table data within a table in a table

I am trying to get specific contents of the second "datadisplaytable" is the Schedule type and Instructors name. The line below:
datadisplaytable = soup.find(class_='datadisplaytable').text
gets me the whole class which contains all other 'datadisplaytable's which I intend to loop through for the specific data that I need.
Using "Xpath" in selenium only gets me the contents of the selected path. and trying to use a for loop in selenium returns "WebElement not iterable'
Which brings me to the question,
How do I get the schedule type and Instructor.
catalog = https://prod-ssb-01.dccc.edu/PROD/bwckschd.p_disp_dyn_sched
term and subject is any.
Use find_elements_by_xpath (‘<your xpath>) not find_element_by_xpath. See the difference one is with s.
Remember find_elements return a list of web elements which you can loop through where as find_element return a single web element.

How to read the value within a button via Selenium?

I am working on a script that aims "taking all of the entries which are written by users" under a specific title in a website(In this case, the title is "python(programlama dili"). I would like to read the number which shows the current number of pages under this specific title.
The reason behind reading the number of this elements is that number of pages can increase at the time when we run the script due to increasing number of entries by users. Thus, I should take the number which exist within the element via script.
In this case, I need to read "122" as the value and assign it to a int variable . I use Selenium to take all entries and Firefox web driver.
It Would be better if you try to access it using the xpath.
Try and get the value attribute of the element, you've mentioned you can find the element using xpath so you can do the following.
user_count = element.get_attribute('value')
If that gets you the number (as a string) then you can just convert to an int as usual
value = int(user_count)
First pick the selector .last and then you can extract the reference of that. Don't forget to split the reference.
my_val = driver.find_element_by_css_selector(".last [href]").split("p=")[1]

getting txt from multiple spans with python selenium

I would like to get the text value of a span class "currency-coins value" to be used in a comparison.
Basically I want to check the market value of a specific player. I get the player listed 20 times in a container. So the "currency-coins value" is shown 20 times on the page.
Now I need to get the "200" as shown in the screenshot of the HTML code above as value I can work with. And this for all 20 results on the page. The value might be different for all 20 results.
After I got all 20 values, I want to check which one is the lowest.
I will then afterwards use the lowest value as price to list my element on the market.
Is there a way to do this? Since I am learning python for a bit more than one week now, I cant figure it out myself.
The idea is to first iterate over the player containers - usually, these are table rows, and, for each container, locate that price element within. For instance:
for row in driver.find_elements_by_css_selector("table tbody > tr"):
coin_value = float(row.find_element_by_css_selector(".currency-coins.value").text)
print(coin_value)
Note that table tbody > tr is used as an example, your locator for table rows or player containers is likely different.

Categories

Resources