硒 - xpath,查找clsass名称不起作用的元素

发布于 2025-01-22 04:53:59 字数 1855 浏览 0 评论 0原文

我正在尝试在门户网站中选择带有名称“ basin”的广播按钮: https:// .gov.in/wris/#/地下水。我正在使用硒来做到这一点。我通过更改类名,XPath等进行了不同的尝试。但是,我遇到了错误:'消息:没有这样的元素:无法找到元素:'。 以下是我尝试过的代码。

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()

I am trying to select the radio button with name 'Basin' in the portal: https://indiawris.gov.in/wris/#/groundWater . I am using selenium for doing that. Different attempts I have made by changing the class name, xpath etc. However, I am getting the error: 'Message: no such element: Unable to locate element:'.
Following is the code I have tried.

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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陪我终i 2025-01-29 04:53:59

为了单击盆地,您需要首先切换到所需的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

屏幕截图:

“

to be able to click on Basin, you need to first switch to desired iframe. Because it is wrapped inside an iframe.

Code:

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

Imports:

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

Output:

Switched successfully to iframe
Clicked on basin button

Process finished with exit code 0

Screenshot:

Execution_result

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文