【问题标题】:How get the selected text option with Selenium using Python?如何使用 Python 使用 Selenium 获取选定的文本选项?
【发布时间】:2020-12-25 23:44:07
【问题描述】:

我在网页中有这个选项

screenshot html code

我正在尝试使用以下代码提取 SELECTBOX 中的选定文本:

time.sleep(5)
elemento2 = Select(WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.NAME, "jform[cod_comune]"))))
selected_option = elemento2.first_selected_option
print(selected_option.text) 

但我什么也没得到,甚至没有错误。文本为空。

screenshot html code full page

【问题讨论】:

    标签: python selenium google-chrome option gettext


    【解决方案1】:

    我不会使用第一个选定的属性。我建议获取 span 标签的 xpath。然后使用 selenium .text 获取该字段的文本。这适用于大多数网站

    selected_option = elemento2.text

    基本方法:

    driver.get(url)
    

    时间.sleep(3) text = driver.find_element_by_xpath(xpath).text

    【讨论】:

    • 我已经阅读了很多网站,但我不明白要设置哪个 XPATH。页面完整截图为screenshot fullpage
    • 你想找到一个标签的xpath,对吧?
    • 是的,我不知道要使用哪个 XPATH
    • 右键span标签,选择copy xpath
    • 我用这个 {elemento2 = Select(WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//*[@id='jform_cod_comune_chosen']/a/span ")))) print(elemento2.text)},但请参阅“raise UnexpectedTagNameException(selenium.common.exceptions.UnexpectedTagNameException: Message: Select only works on
    猜你喜欢
    • 2015-10-11
    • 2015-09-01
    • 1970-01-01
    • 2010-11-26
    • 2013-02-05
    相关资源
    最近更新 更多