简体   繁体   中英

Automate Login with Selenium and Python

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=">

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM