user12453183
user12453183

Reputation:

How to check if URL contains something

I'm making a program which goes to a url, clicks a button, checks if the page gets forwarded and if it does saves that url to a file.

However, after a couple of entries the page blocks you from doing anything. When this happens the URL changes and you'll get this Block.aspx?c=475412

Now how'd I be able to check if the url contains Block.aspx?c=475412 after each try? I've tried looking for this but I could only find people asking how to get the current URL, not what I'm looking for, I need to check what the url contains.

Here is my code.

import selenium
from selenium import webdriver


url_list = open("path')

try:
    driver = webdriver.Chrome("C:\\python\\chromedriver")
    for url in url_list:
       driver.get(url)
       send = driver.find_element_by_id("NextButton")
       send.click()

       if (driver.find_elements_by_css_selector("a[class='Error']")):
          print("Error class found")


except ValueError:
    print("Something went wrong checking the URL.")

I suppose I'd add an if statement checking if the URL contains Block.aspx?c=475412, if anyone would be able to help me out I'd greatly appreciate it.

Upvotes: 1

Views: 12178

Answers (1)

CEH
CEH

Reputation: 5909

If you want to check what the URL contains, you can just use the in method built in with Python strings.

if "Block.aspx?c=475412" in driver.current_url: # check if "Block.aspx?c=475412" is in URL
    print("Block.aspx is in the URL")

Upvotes: 6

Related Questions