1

I just want to perform a simple click action. When I fire up the webdriver the logon page comes on I can find by element name and pass my credentials through and hit enter with no issues.

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.webdriver import WebDriver
from selenium.webdriver.remote.webelement import WebElement

driver = webdriver.Firefox(executable_path=r"C:\arcgispro-py3-clone2\Lib\site-packages\selenium\webdriver\firefox\geckodriver.exe")

driver.get("http://server.domain:9999/landing_logon_page)

print(driver.title) # Title of the page

element_userid = driver.find_element_by_name("userid").send_keys("myusername")
element_pwd = driver.find_element_by_name("pwd").send_keys("mypassword")
element_SignIn = driver.find_element_by_name("Submit").click()
# WORKS FINE UP TO HERE

time.sleep(20)
Element = driver.find_element_by_xpath('//*[@id="SM_PF_WRK_SM_PF_SUBACTION"]').click()

No matter which method of location I use for the last line nothing works. That includes element name, id, css selector and xpath.

Here is the HTML in the div housing the input type for the desired element:

<div id="win1divSM_PF_WRK_SM_PF_SUBACTION">
    <input type="radio" name="SM_PF_WRK_SM_PF_SUBACTION" id="SM_PF_WRK_SM_PF_SUBACTION" class="PSRADIOBUTTON" tabindex="285" value="NEW" checked="checked" onclick="setupTimeout2();addchg_win1(this);submitAction_win1(this.form,this.id);">
    <label for="SM_PF_WRK_SM_PF_SUBACTION" id="SM_PF_WRK_SM_PF_SUBACTION_LBL" class="PSRADIOBUTTON">New Employee</label>
</div>

Here is the error I'm receiving:

Traceback (most recent call last):
  File "<string>", line 22, in <module>
  File "C:\arcgispro-py3-clone2\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "C:\arcgispro-py3-clone2\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
    'value': value})['value']
  File "C:\arcgispro-py3-clone2\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\arcgispro-py3-clone2\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: //*[@id="SM_PF_WRK_SM_PF_SUBACTION"]

Also tried contains:

driver.find_element_by_xpath('//*[contains(@id,"SM_PF_WRK_SM_PF_SUBACTION") and contains(@class, "PSRADIOBUTTON")]').click()

and got this error:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\arcgispro-py3-clone2\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "C:\arcgispro-py3-clone2\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
    'value': value})['value']
  File "C:\arcgispro-py3-clone2\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\arcgispro-py3-clone2\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: //*[contains(@id,"SM_PF_WRK_SM_PF_SUBACTION") and contains(@class, "PSRADIOBUTTON")]

Any ideas on what I need to do to click this button?

5
  • Please add the error that is returned.
    – Jortega
    Commented Jul 13, 2021 at 21:04
  • Can you share the link to that page or at least all the HTML code from the body till that element? Also, possibly element attributes are changing dynamically.
    – Prophet
    Commented Jul 13, 2021 at 21:31
  • @Jortega sorry. added the error message
    – geoJshaun
    Commented Jul 13, 2021 at 21:54
  • @Prophet The HTML code is quite expansive so I just sent the div encasing the element. Link is secured behind firewall so it would do you no good.
    – geoJshaun
    Commented Jul 13, 2021 at 21:55
  • I understand. Are you sure there is no iframe there? Also, try tracking that element attribute values. Possibly they are changing when element is selected etc.
    – Prophet
    Commented Jul 13, 2021 at 22:03

3 Answers 3

0

Check if there is an iframe there.
If yes - you have to switch to the iframe in order to access elements inside it.
And switch to the default content in order to access elements out of that iframe.
See here or here or in any other similar posts about this issue.

1
  • There was an iframe after all. Sorry. I suppose this is a redundant post then.
    – geoJshaun
    Commented Jul 13, 2021 at 23:23
0

You can use combination of @class and @id ... something like : driver.find_element_by_xpath('//*[contains(@id,"id_value") and contains(@class, "class_value")]').click()

2
  • Tried your solution and getting -- Unable to locate element: //*[contains(@id,"SM_PF_WRK_SM_PF_SUBACTION") and contains(@class, "PSRADIOBUTTON")] -- Maybe wrong syntax? All of the identifiers are in my original post within the div and then tag for "input type".
    – geoJshaun
    Commented Jul 13, 2021 at 22:19
  • xpath was in an iframe. Had a hard time finding but eventually was able to. Thanks for the help.
    – geoJshaun
    Commented Jul 13, 2021 at 23:25
0

From what is posted it looks like your code should work so this is just to help troubleshoot.

First lets try using expected conditions.

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.webdriver import WebDriver
from selenium.webdriver.remote.webelement import WebElement

driver = webdriver.Firefox(executable_path=r"C:\arcgispro-py3-clone2\Lib\site-packages\selenium\webdriver\firefox\geckodriver.exe")

driver.get("http://server.domain:9999/landing_logon_page)

print(driver.title) # Title of the page

element_userid = driver.find_element_by_name("userid").send_keys("myusername")
element_pwd = driver.find_element_by_name("pwd").send_keys("mypassword")
element_SignIn = driver.find_element_by_name("Submit").click()
# WORKS FINE UP TO HERE

time.sleep(20)
# Element = driver.find_element_by_xpath('//*[@id="SM_PF_WRK_SM_PF_SUBACTION"]').click()
#########################################################
#         TROUBLESHOOTING
#########################################################
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

element_list = WebDriverWait(driver,10).until(EC.presence_of_all_elements_located((By.XPATH,"//label[@id='SM_PF_WRK_SM_PF_SUBACTION_LBL']")))
print(len(element_list))
# Expected out put is 1
element_to_click = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//label[@id='SM_PF_WRK_SM_PF_SUBACTION_LBL']")))

element_to_click.click()
########################################
# Also try below if above does not work
########################################

element_to_click2 = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//label[contains(text(), 'New Employee')]")))
element_to_click2.click()
1
  • Thanks for the help. Turns out there was an iframe.
    – geoJshaun
    Commented Jul 13, 2021 at 23:24

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.