seleniumbase / SeleniumBase

📊 Python's all-in-one framework for web crawling, scraping, testing, and reporting. Supports pytest. UC Mode provides stealth. Includes many tools.
https://seleniumbase.io
MIT License
5.31k stars 974 forks source link

How to get the child elements of an element. #1973

Closed hbqclh closed 1 year ago

hbqclh commented 1 year ago

in selenium for exmple: parent_div = WebDriverWait(test2, 10).until(EC.visibility_of_element_located((By.XPATH, '//div[@class="el-tabs__header is-top"]'))) child_elements = parent_div.find_elements(By.XPATH, './/div[@class="el-tabs__item is-top"]') How can I achieve similar functionality in seleniumbase? Thank you!

"After I use self.find_element() to get an element, is there a method in seleniumbase to directly click on this element? For example, in selenium: login_button = self.driver.find_element(By.XPATH, '//span[@class=""]') login_button.click()

Thank you!

mdmintz commented 1 year ago

You can use self.click(selector) to directly click on an element via selector. It's more reliable than splitting the actions of finding and clicking into separate commands. Eg:

element = self.find_element(selector)
element.click()

You can also use self.click_visible_elements(selector) instead of this:

elements = self.find_elements(selector)
for element in elements:
    element.click()

Once you have found a WebElement, standard API rules apply (it doesn't have the SeleniumBase API). To use the SeleniumBase API for finding a child element or elements, use the full selector of the child element when calling self.find_element(selector) or self.find_elements(selector).

You can also combine the SeleniumBase API with the WebElement API. Eg:

elements = self.find_elements(SELECTOR1)  # SeleniumBase API
for element in elements:
    child_element = element.find_element("css selector", SELECTOR2)  # WebElement API
    child_element.click()  # WebElement API