Python 3.X Selenium自动登录失败

时间:2017-01-21 21:54:38

标签: python selenium selenium-webdriver selenium-chromedriver

我试图自己做一个自动登录。这是我的代码:

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


browser = webdriver.Chrome()
browser.get('http://omega-pentest.borec.cz/')
wait = WebDriverWait(browser, 10)

idElem = wait.until(EC.visibility_of_element_located((By.ID, "prom1")))
idElem.send_keys('cyka')
idElem.click()


pwElem = wait.until(EC.visibility_of_element_located((By.ID, "prom2")))
pwElem.send_keys('bliat')
pwElem.click()

我一直收到以下错误:

  

Traceback(最近一次调用最后一次):文件   " C:\ Users \ Duchy \ Desktop \ web.py",第11行,in       idElem = wait.until(EC.visibility_of_element_located((By.ID," prom1")))文件   " C:\用户\公国\应用程序数据\本地\程序\的Python \ Python35-32 \ lib中\站点包\硒\的webdriver \支持\ wait.py&#34 ;,   第80行,直到       raise TimeoutException(message,screen,stacktrace)selenium.common.exceptions.TimeoutException:Message:

2 个答案:

答案 0 :(得分:0)

prom1prom2这两个元素不是By.ID(),而是By.Name()。您可以从源代码中看到,没有id属性:

<input name="prom1" required="" type="text">
<input name="prom2" required="" type="password">

此外,两个.click()语句完全没有必要,可以删除。

答案 1 :(得分:0)

首先,我喜欢这里的俄语诅咒词:D 要解决此问题,请尝试使用XPATH代替ID,例如:

import selenium
import shutil
import xlsxwriter
import os
import unittest
import requests
import subprocess
import getpass
import platform
import socket
import ssl
import pynput
import logging
import time 
from pynput.keyboard import Key, Controller
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait 
from datetime import date

import json
with open('secrets.json','r') as f:
      config = json.load(f)

# WebDriver Path for OS.System
if platform.system() == ('Windows'):
    browser = webdriver.Chrome("C:\Program Files (x86)\Google\Chrome\chromedriver.exe")
elif platform.system() == ('Linux'):
    browser = webdriver.Chrome(executable_path='/home/rbarrett/Drivers/Google/Chrome/chromedriver_linux64/chromedriver')
elif platform.system() == ('Darwin'):
    browser = webdriver.Chrome(executable_path='~/Drivers/Google/Chrome/chromedriver_mac64/chromedriver')
else:
    print("Are you sure you have the Selenium Webdriver installed in the correct path?")

# TearDown Method
def tearDown(self):
    self.browser.close()

# ShutDown Method 
def shutDown(self):
    self.browser.quit()

# Parent URL
#browser.get(config['URL']['target_url'])

# Credentials NEEDS UNIT TEST
username = browser.find_element_by_id("login")
password = browser.find_element_by_id("password")
username.send_keys(config['user']['name'])
password.send_keys(config['user']['password'])

# Authentication submit.click()
# For XPATH = //*[@id='bLogin']
element = WebDriverWait(browser, 20).until(
    EC.element_to_be_clickable((By.XPATH, "//*[@id='bLogin']")))
element.click();
print("Logging into <insert_program>!")
print("Authenticated")

我在这里使用其他导入来解决自己的编码问题,但是如果您愿意,可以在此处找到一些示例:https://github.com/Richard-Barrett/ITDataServicesInfra/tree/master/Python