单击 find_element_by_class_name 按钮不起作用 python selenium webdriver 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43021434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Click button by find_element_by_class_name not working python selenium webdriver NOT working
提问by thecpaptain
I'm trying to add contacts on LinkedIn using Python and Selenium. I'm attempting to do so by adding the contact suggestions made by LinkedIn in the "Network" tab (https://www.linkedin.com/mynetwork), which has an infinite scroll feature.
我正在尝试使用 Python 和 Selenium 在 LinkedIn 上添加联系人。我试图通过在具有无限滚动功能的“网络”选项卡 ( https://www.linkedin.com/mynetwork) 中添加 LinkedIn 提出的联系人建议来实现此目的。
Basically I want the script to locate the button "Connect", which is next to each suggested profile, click the button, and then repeat until error whereby the script should scroll down to load more "Connect" buttons to reiterate.
基本上我希望脚本找到每个建议配置文件旁边的“连接”按钮,单击该按钮,然后重复直到出现错误,脚本应向下滚动以加载更多“连接”按钮以进行重申。
The best way I've found to locate the button element is by find_element_by_class_name() since all the connect buttons have the same class. I've also tried locating the elements using CSS and Xpath, without success.
我发现找到按钮元素的最佳方法是通过 find_element_by_class_name() ,因为所有连接按钮都具有相同的类。我还尝试使用 CSS 和 Xpath 定位元素,但没有成功。
PROBLEM: The script is able to click the first Connect button, but none after that. I've tried many ideas for implementation (locating by Xpath, CSS, using a list of buttons to click), yet none seem to work. Below is the relevant part of the script.
问题:脚本能够点击第一个连接按钮,但之后没有。我已经尝试了很多实现的想法(通过 Xpath、CSS 定位,使用按钮列表来单击),但似乎都没有工作。下面是脚本的相关部分。
while True:
try:
driver.find_element_by_class_name("mn-person-card__person-btn-ext.button-secondary-medium").click()
time.sleep(1)
except:
pass
print("trying to scroll")
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(1)
Any ideas? To me it seems as if the code should work, and as if there is something else which is preventing success. Maybe a bug or similar. Might mention that I'm rather new to all of this, and it's the first script I'm trying to make to manipulate a browser.
有任何想法吗?对我来说,似乎代码应该可以工作,并且好像还有其他东西阻止了成功。也许是一个错误或类似的。可能会提到我对这一切都很陌生,这是我尝试制作的第一个脚本来操纵浏览器。
I'm using Firefox driver. Full script can be found here: http://pastebin.com/qtdNsRtz
我正在使用 Firefox 驱动程序。完整脚本可以在这里找到:http: //pastebin.com/qtdNsRtz
Thanks in advance!
提前致谢!
回答by Amit Verma
You should use find_elements
for finding all elements with same class
Try this to get all elements:
您应该find_elements
用于查找具有相同类的所有元素试试这个以获取所有元素:
elements = driver.find_elements_by_class_name("mn-person-card__person-btn-ext.button-secondary-medium")
then use a for loop to click each of them. For example:
然后使用 for 循环单击它们中的每一个。例如:
for e in elements:
e.click()
回答by NarendraR
The way you are trying to use find_element_by_class_name
locator is not correct as this locator doesn't support compound classes within.
您尝试使用find_element_by_class_name
定位器的方式不正确,因为该定位器不支持其中的复合类。
You need to use either xpath
or cssSelector
if class attribute have more then one class name :
您需要使用xpath
或者cssSelector
如果类属性具有多个类名:
driver.find_element_by_xpath("//button[@class='mn-person-card__person-btn-ext button-secondary-medium']").click()