Selenium - xpath,按 clsass 名称查找元素不起作用

Selenium - xpath, find element by clsass name not working

我正在尝试 select 门户中名称为 'Basin' 的单选按钮:https://indiawris.gov.in/wris/#/groundWater。我正在使用硒来做到这一点。我通过更改 class 名称、xpath 等进行了不同的尝试。但是,我收到错误:'Message: no such element: Unable to locate element:'。 以下是我试过的代码。

from http.server import executable

# from matplotlib import image
from selenium import webdriver
from selenium.webdriver.firefox.service import Service

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import sys

# from selenium.webdriver.chrome.service import Service

path2 = r"F:\chromedriver_win32\chromedriver.exe"
s=Service(r"C:\Users\DELL\OneDrive - Technological University Dublin\Desktop\geckodriver-v0.30.0-win64\geckodriver.exe")


import time
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Chrome(executable_path=path2)
driver.maximize_window()
url = 'https://indiawris.gov.in/wris/#/groundWater'
driver.get(url)
time.sleep(10)

# basinId = driver.find_element_by_xpath('/html/body/app-root/main/app-gis/as-split/as-split-area[1]/div/div/div[2]/div[1]/div[2]/div[1]/div/div[3]/div[2]/div/groundwater-sidebar/div/div[1]/label[2]')
completeBox = driver.find_element_by_class_name('calcite')
ngStarsBox = completeBox.find_element_by_class_name('views')
print("ngstarss box length",ngStarsBox)
ngStars = completeBox.find_elements_by_class_name("ng-star-inserted")
# basin = completeBox.find_element_by_link_text('Basin')
print("length is:",len(ngStars))

# itemsinBasin =basinId.find_elements_by_class_name("ng-star-inserted")
print("teeeeeeeeeeeeeeeeeeee",ngStars[3])

actionChains = ActionChains(driver)
actionChains.click(ngStars[4]).perform()

为了能够点击 Basin,您需要先切换到所需的 iframe。因为它被包裹在一个 iframe 中。

代码:

driver.maximize_window()

wait = WebDriverWait(driver, 30)

driver.get("https://indiawris.gov.in/wris/#/groundWater")

try:
    wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe[@class='ng-star-inserted']")))
    print('Switched successfully to iframe')
    ele = wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@class='views']//label[contains(.,'Basin')]//input")))
    driver.execute_script("arguments[0].click();", ele)
    print('Clicked on basin button')
except:
    print('Something went wrong')
    pass

进口:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

输出:

Switched successfully to iframe
Clicked on basin button

Process finished with exit code 0

截图: