我正在使用硒。我想自動化一個網路表單。有兩個下拉選單,我可以自動化第一個但我不能自動化第二個。第一個用于選擇州,第二個用于選區。我已經嘗試了 selenium select 提供的所有三種方法。但它總是給我這個錯誤:無法找到帶有可見文本的元素: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')
uj5u.com熱心網友回復:
您錯過了選擇專案之間的延遲。
選擇狀態后,第二個下拉串列選單需要一些時間來填充它的串列。
所以最簡單的解決方案是在那里添加一些延遲,如下所示:
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')
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/414626.html
標籤:
