0

我知道有几个硒选项选择的例子。尽管如此,我仍然无法选择一个特定的网站。 https://www.gks.ru/dbscripts/munst/munst20/DBInet.cgi 我想在左上角选择 Excel 选项。HTML 是 n 个附件

在此处输入图像描述

我试着这样靠近酒吧

for option in el.find_elements(By.TAG_NAME,'option'):
    print(option.text)
    if option.text == 'CSV':
        option.click() # select() in earlier versions of webdriver
        break

我还按类和 css_selector 使用了 find_elements

然后我用

select = Select(driver.find_element(By.TAG_NAME,'Select'))
select.select_by_visible_text('Excel')

它也找不到元素

有人可以帮忙吗?

4

1 回答 1

2

要使用Selenium标记中选择文本为CSV,您需要诱导WebDriverWait,并且您可以使用以下任一Locator Strategieselement_to_be_clickable()

  • 使用CSS_SELECTOR

    select = Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "select.Select[name='Format']"))))
    select.select_by_visible_text("CSV")
    
  • 使用XPATH

    select = Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//select[@class='Select' and @name='Format']"))))
    select.select_by_visible_text("CSV")
    
  • 注意:您必须添加以下导入:

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

参考

您可以在以下位置找到一些相关的讨论:

于 2021-11-16T22:17:27.870 回答