Selenium Python单击徽标按钮

Selenium Python单击徽标按钮,python,selenium,selenium-webdriver,css-selectors,Python,Selenium,Selenium Webdriver,Css Selectors,在下载了一些内容后,我试图点击一个logo按钮,以返回到原始页面。但它不工作,并引发以下异常: NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"brand.brand-bv"} 结构如下: <a class="brand brand-bv" href="https://workbench-c4.bazaarvoice.com"&g

在下载了一些内容后,我试图点击一个logo按钮,以返回到原始页面。但它不工作,并引发以下异常:

NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"brand.brand-bv"}
结构如下:

<a class="brand brand-bv" href="https://workbench-c4.bazaarvoice.com">
  <span class="visuallyhidden">Bazaarvoice:</span>
</a>
logo_button = driver.find_element_by_css_selector("brand.brand-bv")                                                      
logo_button.click()

品牌
也是类,如果您使用的是
css\u选择器
它需要有前导的

driver.find_element_by_css_selector(".brand.brand-bv")
brand.brand bv
表示带有标签的元素
brand
brand bv
类别

<brand class="brand-bv"/>
你需要导入

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions

您还可以尝试使用xpath实现同样的功能,因为xpath的使用更加灵活, 找到元素并将其存储在var e中,然后检查其长度(如果存在),然后仅单击,您还可以尝试在实际单击它之前使用wait for元素

e = driver.find_element_by_xpath("*//a[href='https://workbench-c4.bazaarvoice.com']")

if len(e) > 0
    e[0].click()
语法是

driver.find_element_by_xpath("*//TAGNAME[ATTRIBUTENAME='ATTIBVALUE']")

非常感谢。但引发了相同的异常…”NoSuchElementException:没有这样的元素:无法定位元素:{“方法”:“css选择器”,“选择器”:.brand.brand bv“}”@user7362809更新了我的答案。这次超时异常:(@user7362809 mmm…你能检查一下元素是否在
标记内吗?它不是…我用一个屏幕截图更新了我的帖子,其中的结构是添加星号作为前导符号,这没有多大意义。实际上抛出了异常:“NoTouchElementException:没有这样的元素:无法找到元素:{“方法”:“xpath”,“选择器”:”//a[href=“}”
driver.find_element_by_xpath("*//TAGNAME[ATTRIBUTENAME='ATTIBVALUE']")