Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checkbox using xpath not accessible

I am trying to access a checkbox titled "Cooperative Agreement (968)" on a website (grants.gov). The URL is this.

It looks like there are two HTML elements on the HTML page. The second HTML is contained in #document.

I don't know what #document means, but I think it is where the issue is, because, I can get anything outside of #document, but nothing inside of it.

This is my current code:

driver.maximize_window()
driver.implicitly_wait(20)
driver.get('https://www.grants.gov/web/grants/search-grants.html')

time.sleep(3)
print('go')
cooperative_agreement = driver.find_element_by_xpath(
'/html/body/table[1]/tbody/tr/td[1]/div/div[1]/div/div/table/tbody/tr[2]/td/input')

print(cooperative_agreement.text)

I am using dev tools on chrome to get the XPath for everything. If you go to the URL and go to dev tools on chrome and try to get the XPath for the checkbox titled "Cooperative Agreement (968)" you will see the issue I am having.

This is my first question on StackOverflow. Please let me know if you need more info on something! Thank you all for your help!

like image 594
StillDontKnowWhatImDoing Avatar asked Apr 26 '26 07:04

StillDontKnowWhatImDoing


1 Answers

You got it correct the element is inside iframe you need to switch it first.

Induce WebDriverWait() and wait for frame_to_be_available_and_switch_to_it() Induce WebDriverWait() and wait for element_to_be_clickable()

WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe#embeddedIframe")))  

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//td[.//label[contains(., 'Cooperative Agreement')]]/input[1]"))).click()

#Jump out from iframe
driver.switch_to.default_content()

You need to import below libraries.

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
like image 163
KunduK Avatar answered Apr 27 '26 21:04

KunduK



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!