Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.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
如何使用Selenium处理Python try/except_Python_Selenium_Exception_Instagram - Fatal编程技术网

如何使用Selenium处理Python try/except

如何使用Selenium处理Python try/except,python,selenium,exception,instagram,Python,Selenium,Exception,Instagram,我正在尝试使用Selenium来跟踪Instagram上的用户 但我的大问题是,当迭代所有配置文件以区分公共/私人用户时(由页面上的不同元素引起)。 在我的代码上,当循环到达私有配置文件时,除外在尝试之后首先开始 我的代码: followers = [ #url of all users] for follower in followers: #Iterate into the list browser.get(follower) sleep(2) try:

我正在尝试使用Selenium来跟踪Instagram上的用户

但我的大问题是,当迭代所有配置文件以区分公共/私人用户时(由页面上的不同元素引起)。 在我的代码上,当循环到达私有配置文件时,
除外
尝试之后首先开始

我的代码:

followers = [ #url of all users]

for follower in followers:
    #Iterate into the list
    browser.get(follower)
    sleep(2)
  
  try:
     browser.find_element_by_css_selector("li:nth-of-type(2) a.-nal3 span").get_attribute("title"):
     print("Public profil") 
     #click on follow                  
  
  except NoSuchElementException:
     print("private profil")
     follower_count = browser.find_element_by_css_selector("li:nth-of-type(2) span.-nal3 span").get_attribute("title")
     if follower_count == 0:
        pass
        print("0 followers")
      
     else:
        print("go follow")
我认为理解正确,如果执行了
except
对应子句,则在语句
try
之后继续执行,但这并不能解释为什么循环在
except
之后结束

错误:

在你的NoTouchElementException中,你又在写作了

     follower_count = browser.find_element_by_css_selector("li:nth-of-type(2) span.-nal3 span").get_attribute("title")

我想这会引发另一个NoTouchElementException,因为您正在再次尝试查找相同的元素。

它不完全相同。一个是
span.-nal3 span
,另一个是
a.-nal3 span
。一开始我自己也这么想,也许是打字错误
a.-nal3
和just
-nal3
@vitali不是同一个公共用户:
a.-nal3 span
;对于私人用户:
-nal3 span