我正在尝试使用for循环动态选择元素。为了让我确定范围,我需要知道元素的数量,我如何在selenium(python)中做到这一点。我需要为此执行javascript吗?
# count = how??
for i in range(1, count): #element index start at 1
xpath = '//div[@class="form-group"]/select/option['+str(i)+']'
if self.find_elements_by_xpath(xpath).text == driver_name:
self.find_elements_by_xpath(xpath).click()
答案 0 :(得分:2)
我相信你试图在选择字段上没有选项,如果是这样,试试这个
from selenium.webdriver.support.select import Select as WebDriverSelect
s_element = WebDriverSelect(driver.find_elements_by_xpath(your_xpath_expression))
count = len(s_element.options)