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
Related
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
I am using selenium in python with chrome driver.
Here is my scenario:
From a web page I extract all elements having id==customer_info or class==prize_info using the following code:
customer_or_prize_list=driver.find_elements_by_xpath('//*[#id="customer_info" or #class="prize_info"]')
Now I want to go through each element of the list 'customer_or_prize_list' and process as follows
if web element contains id==customer_info then do multi_line_text_formatting (assume it as action 1)
if web element contains class==prize_info then do single_line_text_formatting (assume it as action 2)
You can assume that a web element will not contain both id and class.
How can I process list elements using 1. and 2. above?
When you go through each element of the list, get the attribute of the element using get_attribute():
for el in customer_or_prize_list:
is_customer = "customer_info" in el.get_attribute("id")
is_prize = "prize_info" in el.get_attribute("class")
See this url, if you want to know more about get_attribute().
Another way,
why don't you divide the list into two small lists, like below:
customer_list=driver.find_elements_by_xpath('//*[#id="customer_info"]')
for e in customer_list:
'''do multi_line_text_formatting'''
prize_list=driver.find_elements_by_xpath('//*[#class="prize_info"]')
for e in prize_list:
'''do single_line_text_formatting'''
I'm still trying to make a bot with python and selenium. I'm trying to click on the element with the highest value. The highest value is stored in the element
variable. I tried this script, but selenium doesn't manage to locate it...
element = driver.find_element_by_xpath('//p[#value="' + value_to_locate + '"]')
driver.execute_script("arguments[0].scrollIntoView;", element)
element.click()
Here is the HTML :
<p data-v-859a1d26="" class="ml-2">632 Bells</p>
Do you have any idea?
From W3Schools:
The value attribute specifies the value of an element.
If you have multiple paragraphs <p> with different values, what you really need is getting all the paragraphs' values, sorting them, and clicking the max.
Get multiple elements using driver.find_elements_by_xpath (note the plural - elements) and then use Python to execute the sorting logic.
element.text will get you the text content of the element.
Maybe your XPath gets screwed up when you are trying to '//p[#value="' + value_to_locate + '"]' try something like "//p[#value='{}']".format(value_to_locate)
Using Selenium to perform some webscraping. Have it log in to a site, where an HTML table of data is returned with five values at a time. I'm going to have Selenium scrape a particular bit of data off the table, write to a file, click next, and repeat with the next five.
New automation script. I've a myriad of variations of get_attribute, find_elements_by_class_name, etc. Example:
pnum = prtnames.get_attribute("title")
for x in prtnames:
print('pnum')
Here's the HTML from one of the returned values:
<div class="text-container prtname"><span class="PrtName" title="P011">P011</span></div>
I need to get that "P011" value. Obviously Selenium doesn't have "find_elements_by_title", and there is no HTML id for the value. The Xpath for that line of HTML is:
//*[#id="printerConnectTable"]/tbody/tr[5]/td/table/tbody/tr[1]/td[2]/div/span
But I don't see a reference to "title" or "P011" in that Xpath.
pnum = prtnames.get_attribute("title")
AttributeError: 'list' object has no attribute 'get_attribute'
It's like get_attribute doesn't exist, but there is some (albeit not much) documentation on it.
Fundamentally I'd like to grab that "P011" value and print to console, then I know Selenium is working with the right data.
P.S. I'm self-taught with all of this, I'm automating a sysadmin task.
I think the problem is that prtnames is a list of element, not a specific element. You can use a list comprehension if you want a list of the attributes of titles for the list of prtnames.
pnums = [x.get_attribute('title') for x in prtnames]
I want to get the inner html of an element (with get_attribute('innerHTML')) but it doesnt have and id or class and there are multiple elements with the same tag name
test1=driver.find_elements_by_tag_name("td")
This gets the whole list of elements with the same tag name but this doesnt work because get_attribute doesnt work with multiple elements
test2=driver.find_element_by_tag_name("td")
this works but gets the very first td elements but i want the second td element
How do i do this correctly?
you can use xpath like below to get the second td of every row in a table.
driver.find_elements_by_xpath("//table/tr/td[2]")
modify xpath to go to your required table if you need it from a particular table.
As per your question the following line of code is returning you the very first td element :
test2=driver.find_element_by_tag_name("td")
To retrieve the text within the second td element you can use either of the following lines of code :
xpath :
test2 = driver.find_element_by_xpath("//table//tr//following::td[2]").get_attribute("innerHTML")
css_selector :
test2 = driver.find_element_by_css_selector("//table > tr > td:nth-last-child(2)").get_attribute("innerHTML")
Note : The last part of the xpath and the css_selector will definitely identify the second <td> element but you may have to require to adjust the initial part as per your HTML DOM