我是编码的初学者,尝试学习使用 selenium 进行网络抓取,我正在一个项目中进行研究,以检查词典用每个单词破解密码需要多长时间。因此,我的代码读取了一个.txt文件,该文件的每一行都包含一个单词,然后将其写入到栏中,它将复制破解它所花费的时间。问题是我无法捕获网页的html代码的一部分,并且需要帮助。
这是我的代码
# This program run spanish dictionary and check how secure password there are
import random
import time
from selenium import webdriver
#Paste here Chromedriver path
CHROMEDRIVERPATH = "C:\Program Files (x86)\chromedriver.exe"
#Paste here dictionary path in .txt format
dictionary = readFile("spanish_dictionary.txt")
date = str(time.strftime("%Y-%m-%dT%H-%M-%S"))
#read files
driver = webdriver.Chrome(CHROMEDRIVERPATH)
#webpage target
driver.get("https://www.security.org/how-secure-is-my-password/")
time.sleep(2)
#Label
writeFile("results_" + date + ".txt","word,time \n")
#File Content
for word in dictionary:
bar = driver.find_element_by_id('password')
bar.send_keys(word)
bar.clear()
timeToCrack = driver.find_element_by_xpath('//*[@id="hsimp"]/div[1]/div[3]/p[2]').get_attribute("class")
result = word + "," + timeToCrack + "\n"
writeFile("results_" + date + ".txt",result)
time.sleep(random.uniform(0.4,1.0))
这是页面的html代码
<p class="result__text result__time">2 hundred microseconds</p>
我在输出文件中得到这个:
word,time
a,result__text result__time
aba,result__text result__time
abaá,result__text result__time
我要这个:
word,time
a,6 hundred picoseconds
aba,4 hundred nanoseconds
abaá,5 milliseconds
你要:
timeToCrack = driver.find_element_by_xpath('//*[@id="hsimp"]/div[1]/div[3]/p[2]').text
Java等效项是:
driver.findElement(By.xpath("//*[@id="hsimp"]/div[1]/div[3]/p[2]").getText();