Select react-select 下拉列表中的项目,在 Python 中使用 SeleniumLibrary

Select item inside a react-select dropdown list with SeleniumLibrary in Python

抱歉各位,我之前问过同样的问题,但我不能应用到我的实际任务中。 (请参阅上一个问题

我遇到了两个麻烦: 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 class="Select-value-label resourceValue" style="margin-left: 5px;" xpath="1">Core</span>
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']") 

Abd 如果您必须与 iframe 外部的元素交互,那么您必须先从 iframe 切换出来:

driver.switch_to_default_content()
//rest of code