无法找到具有可见文本的元素 Selenium Web 自动化
Could not locate element with visible text Selenium web automation
我正在使用硒。我想自动化一个网络表单。有两个下拉菜单,我可以自动执行第一个,但不能自动执行第二个。第一个是针对 select 州的,第二个是针对地区的。我已经尝试了 selenium select 提供的所有三种方法。但它总是给我这个错误:Could not locate element with visible text: RAJNANDGAON
代码如下:
from lib2to3.pgen2 import driver
from sre_parse import State
from tkinter.tix import Select
from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
import time
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), chrome_options=chrome_options)
driver.get("https://ssg2021.in/citizenfeedback")
time.sleep(5)
# selcting states
state_select = driver.find_element(By.XPATH,'//*[@id="State"]')
drp1 = Select(state_select)
drp1.select_by_visible_text('Chhattisgarh')
# selecting district
district_select = driver.find_element(By.XPATH,'//*[@id="District"]')
drp2 = Select(district_select)
drp2.select_by_visible_text('RAJNANDGAON')
您错过了选择项目之间的延迟。
选择状态后,第二个下拉列表菜单需要一些时间来填充它的列表。
所以最简单的解决办法就是在此处添加一些延迟,如下所示:
from sre_parse import State
from tkinter.tix import Select
from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
import time
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), chrome_options=chrome_options)
driver.get("https://ssg2021.in/citizenfeedback")
time.sleep(5)
# selcting states
state_select = driver.find_element(By.XPATH,'//*[@id="State"]')
drp1 = Select(state_select)
drp1.select_by_visible_text('Chhattisgarh')
time.sleep(3)
# selecting district
district_select = driver.find_element(By.XPATH,'//*[@id="District"]')
drp2 = Select(district_select)
drp2.select_by_visible_text('RAJNANDGAON')
我正在使用硒。我想自动化一个网络表单。有两个下拉菜单,我可以自动执行第一个,但不能自动执行第二个。第一个是针对 select 州的,第二个是针对地区的。我已经尝试了 selenium select 提供的所有三种方法。但它总是给我这个错误:Could not locate element with visible text: RAJNANDGAON
代码如下:
from lib2to3.pgen2 import driver
from sre_parse import State
from tkinter.tix import Select
from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
import time
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), chrome_options=chrome_options)
driver.get("https://ssg2021.in/citizenfeedback")
time.sleep(5)
# selcting states
state_select = driver.find_element(By.XPATH,'//*[@id="State"]')
drp1 = Select(state_select)
drp1.select_by_visible_text('Chhattisgarh')
# selecting district
district_select = driver.find_element(By.XPATH,'//*[@id="District"]')
drp2 = Select(district_select)
drp2.select_by_visible_text('RAJNANDGAON')
您错过了选择项目之间的延迟。
选择状态后,第二个下拉列表菜单需要一些时间来填充它的列表。
所以最简单的解决办法就是在此处添加一些延迟,如下所示:
from sre_parse import State
from tkinter.tix import Select
from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
import time
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), chrome_options=chrome_options)
driver.get("https://ssg2021.in/citizenfeedback")
time.sleep(5)
# selcting states
state_select = driver.find_element(By.XPATH,'//*[@id="State"]')
drp1 = Select(state_select)
drp1.select_by_visible_text('Chhattisgarh')
time.sleep(3)
# selecting district
district_select = driver.find_element(By.XPATH,'//*[@id="District"]')
drp2 = Select(district_select)
drp2.select_by_visible_text('RAJNANDGAON')