Python Selenium检测错误?

时间:2016-08-21 03:11:32

标签: python selenium

您好我一直在努力学习硒。

我遇到的问题是没有检测到元素。我正在使用的html代码在此处突出显示https://gyazo.com/a76608d4c4e9fc3bd29412436604c173。那行代码停止了我的程序并给了我错误https://gyazo.com/72fc72e6488578c9c2a50b71311602c5

我用来检测textarea类的代码是

comment=driver.find_element_by_xpath("//form[@class='comment-form no-border-top']/div/*[1]") or driver.find_element_by_xpath("//form[@class='comment-form']/div/*[1]")

因为有时它会在注释表单之间切换无边框和注释表单。

我的想法是使用条件赋值运算符检查,例如if(comment!= none):做某事,但据我所知,python不允许这样做。

有关做什么以及我的xpath是否是可靠方法的任何建议?

1 个答案:

答案 0 :(得分:0)

如果您尝试解决的问题实际上是条件分配,那么您可能想要了解两件不同的事情。

首先,你可以做:

  

如果(评论!=无)

你必须这样做:

comment = Foo()
if comment is None:
    comment = Bar()

其次,如果第一个调用抛出错误而不仅仅是comment,您可能希望尝试分配None。如果是,请尝试使用try

from selenium.common.exceptions import NoSuchElementException
try:
   comment = driver.find_element_by_xpath("//form[@class='comment-form no-border-top']/div/*[1]")
except NoSuchElementException:
   comment = driver.find_element_by_xpath("//form[@class='comment-form']/div/*[1]")