简体   繁体   English

单击带有 Selenium 和 python 的 CSS 元素

[英]Click CSS Element with Selenium and python

I am trying to click a button using Selenium.我正在尝试使用 Selenium 单击按钮。

Below is the code下面是代码

<button class="_11f5fc88e3dec7bfec55f7f49d581d78-scss _238c8d0c29802f43d5fb66614d042cfa-scss" title="Play" aria-label="Play" data-testid="play-button" style xpath="1">

I tried to do this by the css selector:我试图通过 css 选择器来做到这一点:

searchButton = browser.find_elements_by_css_selector('[class="_11f5fc88e3dec7bfec55f7f49d581d78-scss _238c8d0c29802f43d5fb66614d042cfa-scss"]').click()

MY ERROR: AttributeError: 'list' object has no attribute 'click'我的错误:AttributeError:“list”对象没有“click”属性

Am I approaching this wrong?我接近这个错误吗? Im not understanding the error.我不明白这个错误。

Refer this link of how to use different selectors..请参阅此链接了解如何使用不同的选择器。
https://selenium-python.readthedocs.io/locating-elements.html https://selenium-python.readthedocs.io/locating-elements.html

It is recommended to Copy css selector path by inspecting particular element on website then mouse right click -> copy -> copy css path建议通过检查网站上的特定元素然后鼠标右键单击 -> 复制 -> 复制 css 路径来复制 css 选择器路径

or by analyzing the code manually enter path或者通过分析代码手动输入路径

Here p is paragraph tag and class name is content这里 p 是段落标签,类名是内容

element = driver.find_element_by_css_selector('p.content') element = driver.find_element_by_css_selector('p.content')

AttributeError: 'list' object has no attribute 'click' AttributeError: 'list' 对象没有属性 'click'

This error appear because you use .find_elements , it will return a list.出现此错误是因为您使用.find_elements ,它将返回一个列表。

To solve this issue, try this approach:要解决此问题,请尝试以下方法:

  1. Using .find_element , without s .使用.find_element ,没有s
  2. Or still using .find_elements but with index, like this:或者仍然使用.find_elements但带有索引,如下所示:
browser.find_elements_by_css_selector('[class="_11f5fc88e3dec7bfec55f7f49d581d78-scss _238c8d0c29802f43d5fb66614d042cfa-scss"]')[0].click()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM