【问题标题】:Select item inside a react-select dropdown list with SeleniumLibrary in Python在 Python 中使用 SeleniumLibrary 在反应选择下拉列表中选择项目
【发布时间】:2021-01-14 04:17:15
【问题描述】:

对不起,我之前问过同样的问题,但我无法申请我的实际任务。 (请看上一个问题here

我遇到了两个问题: 1/ 基于 SeleniumLibrary 的项目,我不知道如何调用 find_element_by_xpath 或其他常见的东西(导入 webdriver)

2/ 使用下面的这些代码,SeleniumLibrary 告诉我这不是有效的 XPATH 表达式。

driver = SeleniumLibrary
driver.open_browser("abc.com")
driver.find_element("//div[@id='react-select-2--value']").click()
driver.find_element("//[test()='Core'])  --> Error here "str... is invalid XPATH Expression"

3/ 在上一个问题中,我解决了问题(作为人们的答案)

1.

driver.get("https://react-select.com/home")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.basic-single > div.select__control > div.select__value-container"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[contains(@class, 'select__menu')]/div[contains(@class, 'select__menu-list')]//div[contains(@class, 'select__option') and text()='Green']"))).click()

or:
driver.get("https://react-select.com/home")
driver.maximize_window()
driver.find_element_by_xpath("//div[@class='select__value-container select__value-container--has-value css-1hwfws3']").click()
driver.find_element_by_xpath("//*[text()='Green']").click()

@添加span标签

<span class="Select-value-label resourceValue" style="margin-left: 5px;" xpath="1">Core</span>

【问题讨论】:

    标签: python selenium automation dropdown react-select


    【解决方案1】:
    driver.find_element("//[test()='Core']) 
    

    你没有提供标签

    driver.find_element("//*[test()='Core']") 
    

    您可以提供 * 表示任何标签或指定哪个标签输入,div 等

    还要查看元素是否在 iframe 中,如果它在 iframe 中,则必须先切换到它,然后才能与之交互。

    iframe = driver.find_element_by_xpath("//iframe")
    driver.switch_to_frame(iframe)
    driver.find_element("//div[@id='react-select-2--value']").click()
    driver.find_element("//*[test()='Core']") 
    

    如果你必须与 iframe 之外的元素交互,那么你必须先从 iframe 切换出去:

    driver.switch_to_default_content()
    //rest of code
    

    【讨论】:

    • Core 它可能是:driver.find_element("//span[text() ='Core']") 对吗?兄弟
    • 非常感谢您的帮助! @PDHide
    猜你喜欢
    • 2021-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    • 2021-08-17
    • 2020-05-22
    • 1970-01-01
    相关资源
    最近更新 更多