Selenium - xpath, find element by clsass name not working

Viewed 148

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

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

Related