Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.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 TypeError:';非类型';对象不可下标_Python_Selenium - Fatal编程技术网

Python Selenium TypeError:';非类型';对象不可下标

Python Selenium TypeError:';非类型';对象不可下标,python,selenium,Python,Selenium,在一个错误上被困了很长时间,所以我希望有人能帮助我 我有一段代码如下: urls = driver.find_elements_by_xpath('//*[@href]') for url in urls: hopeful = url.get_attribute('ping') print(hopeful) actual = hopeful[31:] driver.get(actual) time.sleep(4) driver.close 运行代

在一个错误上被困了很长时间,所以我希望有人能帮助我

我有一段代码如下:

urls = driver.find_elements_by_xpath('//*[@href]')

for url in urls:

   hopeful = url.get_attribute('ping')

   print(hopeful)

   actual = hopeful[31:]

   driver.get(actual)   

time.sleep(4)

driver.close 
运行代码时,出现以下错误:

TypeError: 'NoneType' object is not subscriptable
在输出中,我预先获得了一些不需要的方面的URL,这就是为什么我试图使用substring函数删除前31个字符,这将留下我的URL,允许我将其传递到
driver.get()


有没有一种方法可以阻止程序返回并尝试子串
none
,这会导致错误?

这可能很幼稚,但为什么不检查none? 您没有提供触发错误的确切行,因此我认为它是
hopofull[31]

urls = driver.find_elements_by_xpath('//*[@href]')
for url in urls:
   hopeful = url.get_attribute('ping')
   if hopeful and len(hopeful) > 31:
       print(hopeful)
       actual = hopeful[31:]
       if actual.startswith('uk.linkedin.com'):
           driver.get(actual)   
time.sleep(4)
driver.close 

非常感谢您,我是Python编程新手,所以问题通常都是通过简单的修复来解决的,比如这个ha!最后一个问题,是否有一种方法可以为这一行指定:driver.get(actual),只迭代开头有字符串“”的行?否则,我会导致一个“Message:stale元素引用:元素未附加到页面文档”错误,再次感谢您。这对我们的帮助是巨大的!我有一个最后的(可能也是非常简单的)请求,之后我应该没有问题。我注意到我的可搜索字符串是“&ved”前面的所有字符。在此之后的字符串中的字符将使字符串不可搜索。有没有一种方法可以让我捕获以“uk.linkedin.com”开头的任何内容,将所有字符封装到但不包括“&ved”和后面的任何字符?我再一次非常感谢你在这件事上的帮助,我导致了一个奇怪的问题。当我运行代码并打印输出时,我希望输入的URL将完美打印。如果我手动复制并粘贴到搜索栏中,它们中的每一个都会返回我希望的结果。但是,如果使用driver.get方法,则只会打开第一个,并且我收到一个“Message:stale element reference:element未附加到页面文档”错误。这有什么原因/解决办法吗@Risadinha@DrijVyas请为您提供更多详细信息的问题创建一个新问题。