我正在嘗試使用 BeatifulSoup4 抓取發送到電子郵件的驗證碼,并將其粘貼到使用 selenium 的驗證欄位中。這是我用來提取元素內部文本的代碼:
soup = BeautifulSoup(driver.page_source)
number_code = soup.find(class_="sms-text").text
verification_field = driver.find_element_by_name("q")
verification_field.send_keys(number_code)
但是,這將刮掉元素內的所有文本,而不僅僅是我需要的驗證碼。誰能告訴我如何剪掉我不需要的文本部分,以便我只得到數字代碼?
PS:這是我在這里的第一篇文章,我是一個完整的新手,所以請放輕松:)
uj5u.com熱心網友回復:
非常感謝您的回答!那行得通。這是我用來解決問題的代碼:
soup = BeautifulSoup(driver.page_source)
number_code = soup.find(class_="sms-text").text
paste_code =
driver.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[3]/input[1]")
paste_code.send_keys(number_code.replace("Your verification code is #", ""))
感謝您抽出時間給我一個簡單的解釋,即使我的問題可以更好地表述:)
uj5u.com熱心網友回復:
我的理解是 verify_field = driver.find_element_by_name("q") 給你整個文本,你只需要一個特定的部分。好吧,您可以使用字串方法來清理文本。例如,如果文本是“您的驗證碼是#####”并且您想要的部分始終位于句子的末尾,您可以使用
verification_field = driver.find_element_by_name("q")
verification_field.replace("Your verification code is ", "")
這將帶回您需要使用 selenium 鍵入的代碼
這是我的第一個答案,但祝你好運。如果您還有其他問題,請回復,我很樂意為您提供幫助!
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/433127.html
上一篇:selenium.common.exceptions.ElementClickInterceptedException:訊息:元素單擊攔截錯誤單擊使用SeleniumPython的單選按鈕
