Selenium Webdriver finding an element in a sub-element
selenium find element within element
how to find child element in selenium webdriver python
selenium get first child element
selenium get number of child elements
selenium find nested element
selenium find element relative to other element
selenium find element by xpath
I am trying to search for an element in a sub-element with Selenium (Version 2.28.0), but selenium des not seem to limit its search to the sub-element. Am I doing this wrong or is there a way to use element.find to search a sub-element?
For an example I created a simple test webpage with this code:
<!DOCTYPE html> <html> <body> <div class=div title=div1> <h1>My First Heading</h1> <p class='test'>My first paragraph.</p> </div> <div class=div title=div2> <h1>My Second Heading</h1> <p class='test'>My second paragraph.</p> </div> <div class=div title=div3> <h1>My Third Heading</h1> <p class='test'>My third paragraph.</p> </div> </body> </html>
My python (Version 2.6) code looks like this:
from selenium import webdriver driver = webdriver.Firefox() # Open the test page with this instance of Firefox # element2 gets the second division as a web element element2 = driver.find_element_by_xpath("//div[@title='div2']") # Search second division for a paragraph with a class of 'test' and print the content print element2.find_element_by_xpath("//p[@class='test']").text # expected output: "My second paragraph." # actual output: "My first paragraph."
If I run:
It returns the html from the second division. So selenium is not limiting its search to element2.
I would like to be able to find a sub-element of element2. This post suggests my code should work Selenium WebDriver access a sub element but his problem was caused by a time-out issue.
Can anyone help me understand what is happening here?
If you start an XPath expression with
//, it begins searching from the root of document. To search relative to a particular element, you should prepend the expression with
element2 = driver.find_element_by_xpath("//div[@title='div2']") element2.find_element_by_xpath(".//p[@class='test']").text
Locating child nodes of WebElements in selenium, If you want all the child Elements recursively , you can replace the above code with MyCurrentWebElement.findElements(By.xpath(".//*"));. It returns the HTML from the second division. So selenium is not limiting its search to element2. I would like to be able to find a sub-element of element2. This post suggests my code should work Selenium WebDriver access a sub element but his problem was caused by a time-out issue. Can anyone help me understand what is happening here?
Use the following:
element2 = driver.find_element_by_cssselector("css=div[title='div2']") element2.find_element_by_cssselector("p[@class='test']").text
Please let me know if you have any problems.
Selenium web driver - how to select child elements, Using xpath to locate an element works regardless of the parents of the element. It doesn't matter if the parent is an li or a span, as long as your� Find Element command takes in the By object as the parameter and returns an object of type WebElement. By object in turn can be used with various locator strategies such as ID, Name, Class Name, XPATH etc. Below is the syntax of FindElement command in Selenium web driver.
This is how you search for element or tag in CSS subclass and I believe that it works for multilevel situation as well:
<li class="meta-item"> <span class="label">Posted:</span> <time class="value" datetime="2019-03-22T09:46:24+01:00" pubdate="pubdate">22.03.2019 u 09:46</time> </li>
Find child elements with locator chaining, Have you ever had to find an element in another element? Maybe you used a locator that searches for the parent element first and then for the child element in the parent. Like in learn Selenium WebDriver � 3 Comments. Find Elements in Selenium WebDriver: Locator Strategy/ Types of locators Locator Strategy can be one of the following types to find an element or FindElements – ID; Name; ClassName; TagName; Link Text/Partial Link Text; CSS Selector; XPATH Selector; Let us now try to see how each of these strategies can be used to find an element or find
Finding Child Elements in WebDriver using findElements, Finding Child Elements in WebDriver using findElements is like a cakewalk for many. This approach is going to take help of Selenium WebDriver Javadocs of � Finding generic Xpath and fetching all element in a list using findElements method and then iterating in the list and printing all the search result using getText() method… So let’s see how to achieve this generic XPath. 1- Open Google.com 2- Search “I want child Element” 3- Right-click on the first search result and select inspect element.
How to Find Elements in Selenium WebDriver, Find Elements in Selenium WebDriver: Why do we need Find Element or FindElements? Locating child elements(direct child/sub child). It takes place AFTER find the Web Element. Parameters: arg0=locator arg1=Web Element arg2=driver (xv) void onException(Throwable arg0, WebDriver arg1) This method is thrown whenever an exception is thrown. For instance, if the Webdriver is not able to find the element, it will trigger this method and would execute whatever code is written
- This is an unfortunate design error in WebDriver that has been enshrined as a feature. All other
element.find_element_by_whatever()methods in WebDriver perform the the search within the context of the specified element. find_element_by_xpath() does not. Selenium bug #403 goes into more detail.
- That's not design error of WebDriver, that's what XPath specification demands.
.textdoesn't return text value for anchor tag. this one does