簡體   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