简体   繁体   English

Python Selenium:如何单击元素

[英]Python Selenium : How to click an element

Program: Python + Selenium 程序: Python + Selenium

Situation: I am trying to run a script to automate Facebook login and open my Facebook home page 现状:我正在尝试运行一个脚本来自动执行Facebook登录并打开我的Facebook主页。

Problem: I am a beginner and i am not sure if there is a problem in my python configuration or i am just doing a code mistake. 问题:我是一个初学者,我不确定我的python配置是否存在问题,或者我只是在执行代码错误。 the code is stopping at the last line. 代码在最后一行停止。 How did I retrieve the element class? 我如何检索元素类? i went to Facebook page and i inspected the profile button element and i copied it but it is not working (_1vp5 f_click) 我去了Facebook页面并检查了配置文件按钮元素,并复制了它,但是它不起作用(_1vp5 f_click)

Resolution: could you please direct me on the best way to find the home page element 解决方法:请您指导我寻找首页元素的最佳方法

Below is my code: 下面是我的代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome(executable_path=r"chromedriver.exe")
driver.get("http://www.facebook.com")

usr = "xxxxx"
pwd = "yyyy"

assert "Facebook" in driver.title
elem = driver.find_element_by_id("email")

elem.send_keys(usr)
elem = driver.find_element_by_id("pass")

elem.send_keys(pwd)
elem.send_keys(Keys.RETURN)

driver.find_element_by_class("_1vp5 f_click").click()

Code output: 代码输出:

Traceback (most recent call last):
File "C:/Users/shouks/MyPythonScripts/test1.py", line 23, in <module>
driver.find_element_by_class("_1vp5 f_click").click()
AttributeError: 'WebDriver' object has no attribute 'find_element_by_class'

There is no such method find_element_by_class . 没有这样的方法find_element_by_class

Use this method instead: find_element_by_class_name 请改用此方法: find_element_by_class_name

http://selenium-python.readthedocs.io/locating-elements.html#locating-elements-by-class-name http://selenium-python.readthedocs.io/locating-elements.html#locating-elements-by-class-name

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

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