Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Selenium if字符串包含精确匹配_Python_Selenium_Selenium Webdriver - Fatal编程技术网

Python Selenium if字符串包含精确匹配

Python Selenium if字符串包含精确匹配,python,selenium,selenium-webdriver,Python,Selenium,Selenium Webdriver,我正在尝试使用python selenium检查URL,以查看站点所在的页面。我有以下网址 http://www.example.com http://www.example.com/page1 http://www.example.com/contact 我正在使用这个python if "http://www.example.com" in url: print("The URL is homepage") else: print("The URL is not homep

我正在尝试使用python selenium检查URL,以查看站点所在的页面。我有以下网址

http://www.example.com
http://www.example.com/page1
http://www.example.com/contact
我正在使用这个python

if "http://www.example.com" in url:
    print("The URL is homepage")
else:
    print("The URL is not homepage")

这不起作用,因为所有URL都包含字符串,如何更改它使其仅适用于精确匹配?

使用相等运算符
==
,如下所示:

if url == "http://www.example.com":
    print("The URL is homepage")
else:
    print("The URL is not homepage")

按照惯例,将变量名放在等式运算符的LHS上,并将测试它的字符串放在RHS上。

使用等式运算符
==
,如下所示:

if url == "http://www.example.com":
    print("The URL is homepage")
else:
    print("The URL is not homepage")

按照惯例,将变量名放在等式运算符的LHS上,并将测试它的字符串放在RHS上。

如果您想更进一步,可以使用

重新导入
a=re.compile('.*example\.com$'))
#.*忽略example.com前面的内容
# \. 逃之夭夭
#$表示这必须是字符串的结尾

如果a.match(url):#如果您想更进一步,可以使用

重新导入
a=re.compile('.*example\.com$'))
#.*忽略example.com前面的内容
# \. 逃之夭夭
#$表示这必须是字符串的结尾
如果a.match(url):#