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.stack.imgur.com/X64k8.png

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

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

Grant28
  • 3
  • 2

1 Answers1

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)
Nic Laforge
  • 1,776
  • 1
  • 8
  • 14
  • 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() – Grant28 Dec 22 '20 at 07:55
  • 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. – Nic Laforge Dec 22 '20 at 08:04
  • 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 " – Grant28 Dec 22 '20 at 08:14
  • The EC is from here "until(EC.element_to_be_clickable((By.CSS_SELECTOR, '[value="E"]')))" – Grant28 Dec 22 '20 at 08:15
  • 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``` – Nic Laforge Dec 22 '20 at 08:16
  • Just noticed I was missing the following - from selenium.webdriver.support import expected_conditions as EC. THANK YOU NIC!!!!!! ITS WORKING!!! – Grant28 Dec 22 '20 at 08:18