【发布时间】:2020-11-28 08:20:27
【问题描述】:
我是编码的初学者,并尝试学习使用 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
【问题讨论】:
标签: python selenium xpath css-selectors webdriverwait