如何检查URL是否包含某些内容

时间:2019-11-28 19:20:47

标签: python selenium

我正在制作一个程序,该程序转到一个URL,单击一个按钮,检查页面是否被转发,以及是否确实将该URL保存到文件中。

但是,在几次输入后,页面阻止您执行任何操作。发生这种情况时,URL就会更改,您会得到这个Block.aspx?c=475412

现在,每次尝试后如何检查URL是否包含Block.aspx?c=475412? 我已经尝试过查找此内容,但我只能找到有人问如何获取当前URL,而不是我要查找的内容,我需要检查该URL包含的内容。

这是我的代码。

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.")

我想我要添加一条if语句来检查URL是否包含Block.aspx?c=475412,如果有人能够帮助我,我将非常感谢。

1 个答案:

答案 0 :(得分:3)

如果要检查URL包含的内容,可以只使用Python字符串内置的in方法。

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")