简体   繁体   中英

How can I make to start a code sequence only if the code finds a certain word on page using Selenium with Python 3.8?

I was trying to make a code that screenshots webpage if only it finds a certain word on this page. I am using Selenium Chrome Webdriver in Python 3.8.

I have tryed that so far, but it seems not to work:

import time
from selenium import webdriver
browser = webdriver.Chrome(PATH)

browser.get('LINK HERE')
time.sleep(10) #so it loads the page
if ('CERTAIN TEXT' in browser.page_source):
    time.sleep(3)
    browser.save_screenshot(PATH AND FORMAT)
    n += 1
    print('CERTAIN TEXT FOUND IN URL')

Regex can do the job:

from selenium import webdriver
import re

driver = webdriver.Chrome()
driver.get("https://stackoverflow.com/questions/61643764/how-can-i-make-to-start-a-code-sequence-only-if-the-code-finds-a-certain-word-on")

# replace "tryed" with your text
if re.search(r"tryed", driver.page_source):
    # found

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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