Code:
FindComment = browser.find_element_by_xpath("//*[contains(text(), 'commented')]").parent
print(FindComment)
Output:
<selenium.webdriver.firefox.webdriver.WebDriver (session="99sf9sfjSIFSf-f9sf(JSFsfsfs")>
Not sure why it prints this. I want it to print the actual element path
https://www.selenium.dev/selenium/docs/api/py/webdriver_remote/selenium.webdriver.remote.webelement.html
parent
Internal reference to the WebDriver instance this element was
found from.
You cannot find the locator from webelement
The only thing you could do is
element.get_attribute("outerHTML")
Related
I'm trying to use relative locators in selenium 4, but I'm not having much luck with it.
I found an example here, but even though I'm able to find the first element, the second line doesn't work and it seems to be stuck there (I cannot close the browser automatically afterwards).
decision_div = browser.find_element(By.CLASS_NAME, "decisions")
conclusion_div = browser.find_element(locate_with(By.TAG_NAME, "div").below(decision_div))
How can I get it to find the next div right below the one with the specified class name?
As you are able to find the first element, I don't see any issues in your code as such:
decision_div = browser.find_element(By.CLASS_NAME, "decisions")
conclusion_div = browser.find_element(locate_with(By.TAG_NAME, "div").below(decision_div))
You just need to ensure that the the element with the value of class attribute as decisions is above the desired <div> element as follows:
<sometagname class="decisions" ...></div>
<div class="some class" ...></div>
Note : You have to add the following imports :
from selenium.webdriver.support.relative_locator import locate_with
References
You can find a couple of relevant detailed discussions in:
How to get the count of likes from a Twitter post using Selenium?
Selenium 4 relative locators are dealing with pair of web elements with the similar size and adjacent positions, not what you are trying to do here.
In this case the div element you are trying to locate is similarly nested below the parent element located by decisions class name.
So, you can simply locate the conclusion_div element with this code line:
conclusion_div = browser.find_element(By.XPATH, "//div[#class='decisions']//div")
But since the locator above gives 13 matches and I don't know which of them you want to locate it could be:
conclusion_div = browser.find_element(By.XPATH, "//div[#class='decisions']//div")
Or maybe
conclusion_div = browser.find_element(By.XPATH, "//*[#class='decisions']//div[contains(#class,'carousel')]")
Or maybe
conclusion_div = browser.find_element(By.XPATH, "//*[#class='decisions']//div[#class='decision-image']")
We can get a parent of a selenium element using xpath, by
par_element = element.find_element_by_xpath('..')
In a similar fashion, how can we get the child of the element? I tried the following and did not work
child_element = element.find_element_by_xpath('/')
child_element = element.find_element_by_xpath('//')
To get to the child of the WebElement you need to set the context to the current element using the dot character i.e. .. So effectively your line of code can be either of the following:
child_element = element.find_element_by_xpath('./')
or
child_element = element.find_element_by_xpath('.//')
i want to get the second child element of content.When i try this code it says the nth-child(2) is not a valid expression. How can i get the child element of content?
content= stream.find_elements_by_class_name("content")
for l in content:
child = l.find_elements_by_xpath("nth-child(2)")
print(child.tag_name)
This solution works for me.
child = l.find_elements_by_xpath("./*[2]").
I'm trying to copy text from comment on a website<span class="auto-link">yes</span> and my python code is
element=browser.find_elements_by_xpath('//span[#class="auto-link"][1]')
print(element.text)
but I keep on getting the 'list' object has no attribute 'text' error, I don't know what I'm doing wrong.
I'm using selenium in python. Try this code I hope this will work for you.
element=browser.find_elements_by_xpath('//span[#class="auto-link"][1]')
for value in element:
print(value.text)
I've never used Selenium, but based on the error and your response, the answer is pretty clear.
When you search for a class, there may be multiple matching elements, so it returns a list of all found matches. Even if you only have a single element with that class, it will still return a list for consistency.
Just grab the first element from the found elements:
elements = browser.find_elements_by_xpath('//span[#class="auto-link"][1]')
# ^ Renamed to reflect type better
print(elements[0].text)
# ^ Grab the first element
# instead of driver.find_elements_by_xpath() use driver.find_element_by_xpath() to get the individual element of the table
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import time
# Google Chrome
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("https://testautomationpractice.blogspot.com/")
# Get Number of Rows
rows = len(driver.find_elements_by_xpath("//*[#id='HTML1']/div[1]/table/tbody/tr"))
# Get Number of Columns
columns = len(driver.find_elements_by_xpath("//*[#id='HTML1']/div[1]/table/tbody/tr[1]/th"))
print("Number of Rows:", rows)
print("Number of Columns:", columns)
# In web table index starts with 1 instead of 0
for row in range(2, rows+1):
for col in range(1,columns+1):
value = driver.find_element_by_xpath("//*[#id='HTML1']/div[1]/table/tbody/tr["+str(row)+"]/td["+str(col)+"]").text
print(value, end=' ')
print()
time.sleep(5)
# Close the Browser
driver.close()
This will work when you are looking for more than one element and takes the first element that matches the xpath:
element=browser.find_elements_by_xpath('//span[#class="auto-link"][1]').get_attribute("innerHTML")
print(element)
This is when you are looking only for one:
element=browser.find_element_by_xpath('//span[#class="auto-link"]').get_attribute("innerHTML")
print(element)
The output:
>>>yes
First Write the xpath of span in which you are currently working and then add the index number in the last of xpath but within it like given below.
from selenium import webdriver`
driver = webdriver.Firefox()
driver.get("http://www.example.org")
element=browser.find_elements_by_xpath('//span[#class="auto-link"[1]').click()
print(element)
[1] is the index number of my value which i want to access.
dont use find_elements_by_xpath, but find_element_by_xpath
There are 10 elements by this selector, it prints all of them
roles = driver.find_elements_by_xpath("(//label[#class='container-checkmark disabled'])")
for x in range(len(roles)):
print(roles[x].text)
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.