How to select the first li element in a selenium test?

The tag structure is shown in the screenshot.

enter image description here

I am trying to use this python code to access the first element li:

invitations = elem.find_element_by_css_selector('members-holder li#invitation-holder > :first-child')

elem is a top-level element containing all tags

I got this exception:

NoSuchElementException: Message: Unable to locate element: {"method":"css selector","selector":"members-holder li#invitation-holder > :first-child"}

I tried several options, but cannot get the result that I want. What is the best way to select the specified item?

EDIT

I tried an alternate css selector .members-holder .invitation-holder:first-childin the developer console in Chrome and it works. But when I plug it in selenium python code, the same exception is thrown.

+4
source share
2 answers

, CSS:

.members-holder ul li.invitation-holder

, li, find_element_by_css_selector():

driver.find_element_by_css_selector('.members-holder ul li.invitation-holder')

, e :

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

...
element = WebDriverWait(driver, 10).until(
    EC.visibility_of_element_located((By.CSS_SELECTOR, ".members-holder ul li.invitation-holder"))
)

, , , iframe - , :

frame = driver.find_element_by_id('frame_id')
driver.switch_to.frame(frame)

# find the element
+3

, .

WebElement parentDiv = driver.findElement(By.className("'members-holder"));

Do

WebElement ul = parentDiv.findElement(By.tagName("ul"));
List<WebElement> lis = ul.findElements(By.tagName("li"));

lis.get(0);

java-. python. , .

+3

All Articles