0

I've been trying to select a radio button using selenium and I'm having no luck. All other selectors (Login, drop down, etc) have all worked fine.

driver.find_element_by_xpath('//*[@id="content_grid"]/div[1]/div[2]/div[4]/div[2]/div[3]/label/div[1]/input').click()

This is the radio button I'm trying to select... [1]: https://i.sstatic.net/X64k8.png

Here is the webpage - https://stathead.com/basketball/pgl_finder.cgi

Appreciate any help! First time poster and noobie coder :)

11
  • xpath is correct in the question. is there specific selenium exception in code? Commented Dec 22, 2020 at 6:49
  • I don't believe so, I'm googling selenium exceptions as we speak! Commented Dec 22, 2020 at 6:51
  • selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element is not clickable at point (173, 1014) Commented Dec 22, 2020 at 6:52
  • can you try xpath //*[@class="fieldset is_playoffs changed"]/div/div[3] Commented Dec 22, 2020 at 6:56
  • Did not work - got this exception. selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@class="fieldset is_playoffs changed"]/div/div[3]"} Commented Dec 22, 2020 at 6:58

1 Answer 1

0

Few options to solve the problem. The problem occur as upon retrieving the element and scrolling down to click on it, the site adds a banner that may obstruct the element.

One option is to always open the browser in fullscreen.

driver = webdriver.Chrome()
driver.maximize_window()

This should help avoiding the banner to be in the way.. less scrolling and less chance for the banner to overlap with the radio button

Selenium also offer a library to return an element upon a condition being met using WebDriverWait

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
el = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '[value="E"]')))

Basically, the above will return the element once it is reported as clickable. The clickability of the element might not be in cause here. This way only ensure that the element found can be clicked but does not validation if there is an overlap (which I suspect is the issue here).

Third option, you may go with javaScript to click on the element

driver.execute_script("arguments[0].click();", el)
Sign up to request clarification or add additional context in comments.

6 Comments

Hey Nic! Thanks for the answer. I tried to use the driver.maximize_window() solution and that did not work. Second and third solution are not working for me either. First Attempt : el = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '[value="E"]'))) driver.execute_script("arguments[0].click();", el) Second Attempt: el = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '[value="E"]'))) el.find_element_by_xpath('//*[@id="content_grid"]/div[1]/div[2]/div[4]/div[2]/div[3]/label/div[1]/input').click()
The el is actually equal to your driver.find_element_by_xpath('//*[@id="content_grid"]/div[1]/div[2]/div[4]/div[2]/div[3]/label/div[1]/input') I have simplified it using css_selector. driver.find_element_by_css_selector('[value="E"]'). The value "E" was obtain. using the inspector and equal to "Either" which I believe is what you are trying to click with your xPath. Once you get the "el" use the javaScript.
Thanks for being so patient, Nic. Upon running the code I'm getting this error. Name 'EC' is not defined. Any idea what I may have to do here? Is there an additional library I should import? I've already done " from selenium.webdriver.support.ui import WebDriverWait "
The EC is from here "until(EC.element_to_be_clickable((By.CSS_SELECTOR, '[value="E"]')))"
Sorry forgot the imports! from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.