-1

I am trying to use Selenium to log into a printer and conduct some tests. I really do not have much experience with this process and have found it somewhat confusing.

First, to get values that I may have wanted I opened the printers web page in Chrome and did a right click "view page source" - This turned out to be not helpful. From here I can only see a bunch of <script> tags that call some .js scripts. I am assuming this is a big part of my problem.

Next I selected the "inspect" option after right clicking. From here I can see the actual HTML that is loaded. I logged into the site and recorded the process in Chrome. With this I was able to identify the variables which contain the Username and Password. I went to this part of the HTML did a right click and copied the Xpath. I then tried to use the Selenium find_element_by_xpath but still no luck. I have tried all the other methods to (find by ID, and name) however it returns an error that the element is not found.

I feel like there is something fundamental here that I am not understanding. Does anyone have any experience with this???

Note: I am using Python 3.7 and Selenium, however I am not opposed to trying something other than Selenium if there is a more graceful way to accomplish this.

My code looks something like this:

EDIT Here is my updated code - I can confirm this is not just a time/wait issue. I have managed to successfully grab the first two outer elements but the second I go deeper it errors out.

def sel_test():
    chromeOptions = Options()
    chromeOptions.add_experimental_option("useAutomationExtension", False)
    browser = webdriver.Chrome(chrome_options=chromeOptions)
    url = 'http://<ip address>/'
    browser.get(url)
    try:
        element = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, '//*[@id="ccrx-root"]')))

    finally: browser.quit()

The element that I want is buried in this tag - Maybe this has something to do with it? Maybe related to this post

<frame name="wlmframe" src="../startwlm/Start_Wlm.htm?arg11=">
4
  • If I'm understanding, you'd like to automate log-in to a url to avoid having to manually enter user/pass information? That's generally not good practice as it undermines security. That said, I have seen it implemented successfully by creating environment variables (eg, printerUSER, printerPASS) and pushing them to the appropriate user/pass webpage element. Commented Feb 5, 2019 at 22:28
  • Can you provide the HTML? That'll help eliminate some common pitfalls (e.g. element is in <iframe>)... Commented Feb 5, 2019 at 22:28
  • If you're dealing with JS you may want to add some wait time in there to let the page fully load. Commented Feb 5, 2019 at 22:30
  • @Hatt this was my first suspicion. I did add a short 30 second timer in there to determine if this may be the issue and still didn't work. Maybe it needs longer? Commented Feb 5, 2019 at 23:20

2 Answers 2

0

As mentioned in this post you can only work with the current frame which is seen. You need to tell selenium to switch frames in order to access child frames.

For example: browser.switch_to.frame('wlmframe')

This will then load the nested content so you can access the children

Sign up to request clarification or add additional context in comments.

Comments

0

Your issue is most likely do to either the element not loading on the page until after your bot searches for it, or a pop-up changing the xpath of the element.

Try this:

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

delay = 3 # seconds
try:
    elementUsername = WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.xpath, 'element-xpath')))

    element.send_keys('your username')

except TimeoutException:
    print("Loading took too much time!")

you can find out more about this here

3 Comments

this code doesn't work as written. Is this for Python2 maybe?
I mostly copied this code from the link at the end of my answer. I personally use selenium with node.js so I can't tell you for sure.
it's using python2 syntax for print

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.