繁体   English   中英

使用 Selenium 在特定文本之后查找所有内容

[英]Finding everything after a specific text using Selenium

我试图找到一个特定的句子和它后面的内容,以便为 selenium 自动化项目输入密码。 基本上每次打开网站时,它都需要密码的三个随机字符在任何 position 我的想法是找到所需字符的最简单方法是找到一个特定的句子“aria-label =”请从密码中输入以下字符, 1, 2 和 8。”

所以到目前为止我设法做的是找到短语“你的密码”,使用;

get_source = driver.page_source
search = "your password,"
print(search in get_source)

我将如何 go 关于在逗号后查找文本

找到文本的元素;

<input type="password" class="pf pf2 ui-keyboard-input-selected vi-activeElement" name="d9ac2374" id="pff1" num="2" onkeyup="checkEntries2(this, event)" ondragstart="return false" onselectstart="return false" tabindex="1" maxlength="1" value="" autocomplete="off" aria-label="Please type the following characters from your password, 2, 3 and 9." aria-labelledby="id_pff1" showmehow="Please type the following characters from your password, 2, 3 and 9. This is password character number 2." aria-invalid="false">

您应该能够使用以下代码获取该信息,前提是该元素具有 ID(在有效的 HTML 页面中是唯一的):

desired_info = WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='pff1']"))).get_attribute('showmehow').split('your password,')[1].split('.')[0].replace (' and', ',')

您还需要以下导入:

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

Selenium 文档: https://www.selenium.dev/documentation/

如果不行,也可以根据部分css选择器定位。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM