0

When I inspect the code of the button it brings me to this section:

<input type="radio" name="chosen" value="UniqueNameExample">

Trying to find element within this that I am able to click(). Using the value results in element not interactable. both the type and name repeat throughout page. Button is loaded and clickable by cursor by time code is ran.

Code I am currently trying:

  time.sleep(3)
   action19 = driver.find_element(By.XPATH, "//input[@value='UniqueNameExample']")
    action19.click()

Also tried:

action19 = driver.find_element(By.XPATH, "//input[@type='radio'][@name='chosen'][@value='UnquieNameExample']")
storyr4
  • 15
  • 5

4 Answers4

0

You can try with CSS selector

driver.find_element_by_css_selector("input[value='UniqueNameExample']").click() 

Also, make sure the element is interactable at the moment you are trying to click on it. Use some waiting strategy to achieve this, for example you can utilize WebDriverWait with ExpectedCondition. You can find more info here.

kalito
  • 1
  • 1
0

Sometimes elements on a website just aren't interactable to selenium. There's a reason why, I just don't know what it is in this specific scenario. However, when an element is being particularly stubborn, I copy it's js path, and run an execute script that uses javascript to click the element instead.

((IJavaScriptExecutor)driver).ExecuteScript("document.querySelector('jsPath').click();");

Josh Heaps
  • 296
  • 2
  • 13
  • Oh interesting I will give that a try! How do I find the jsPath? – storyr4 May 31 '23 at 02:02
  • If you right click on the element in dev tools, hover over copy, and click copy jspath – Josh Heaps May 31 '23 at 14:54
  • I will say, my answer won't work directly as written. I grabbed that from c#. The script itself will work, but I know python will be something more like `driver.execute_script` than `((IJavaScriptExecutor)driver).ExecuteScript` – Josh Heaps May 31 '23 at 14:57
  • 1
    Got it to work with `time.sleep(5) js = "document.querySelector('#bxlist > li:nth-child(5) > div > input[type=radio]').click();" driver.execute_script(js) time.sleep(3)` – storyr4 Jun 01 '23 at 01:58
0

You were close enough. The value of the value attribute sould have been UniqueNameExample instead of UniqueNameExample. Your effective line of code would be:

driver.find_element(By.XPATH, "//input[@type='radio'][@name='chosen'][@value='UniqueNameExample']").click()

Ideally to interact with <input> elements you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[type='radio'][name='chosen'][value='UniqueNameExample']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@type='radio'][@name='chosen' and @value='UniqueNameExample']"))).click()
    
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Updated code the @was in original code. All the code I tried that you recommended still gives me "element not interactable". – storyr4 May 31 '23 at 22:34
0

Try waiting for the element to be clickable:

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

EW = 20  # EW stands for explicit_wait
WebDriverWait(driver, EW).until(EC.element_to_be_clickable(
        (By.XPATH,
         "//input[@value='UniqueNameExample']")
    )).click()
moisesJ
  • 36
  • 5