I am trying to scrape a verification code sent to an email with BeatifulSoup4 and paste it into a verification field using selenium. This is the code that I use to extract the text inside of the element:
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)
However this will scrape all the text inside the element and not only the verification code I need. Can anyone tell how I can cut out the part of the text which I dont need so that I only get the number code?
PS: this was my very first post here and Im a complete newbie so please take it easy on me :)
CodePudding user response:
What I understand is that verification_field = driver.find_element_by_name("q") gives you the entire text and you just need an specific part. Well, you can use strings methods in order to clean the text. For example if the text is "Your verification code is #####" and the part you want is always at the end of the sentence, you can use
verification_field = driver.find_element_by_name("q")
verification_field.replace("Your verification code is ", "")
This will bring you back just the code that you need to type using selenium
This is my first answer but good luck. If you have another problem, reply and I'll be glad to help you!
CodePudding user response:
Thank you so much for your answer! that worked. Here is the code that I used to resolve the issue:
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 #", ""))
I appreciate you taking the time to give me a simple explanation even though my question could have been better formulated :)