1

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.

  • 2
    Check this answer https://stackoverflow.com/questions/3437059/does-python-have-a-string-contains-substring-method - the URL is a property of the WebDriver `driver.current_url` – Dalvenjia Nov 28 '19 at 19:27
  • Thanks this worked, however I don't know how to accept your answer. –  Nov 28 '19 at 19:32
  • i believe you look for a eventlistener, not a current_url – Sk. Nov 28 '19 at 19:34
  • current_url worked. –  Nov 28 '19 at 19:36

1 Answers1

6

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")
CEH
  • 5,701
  • 2
  • 16
  • 40