python点击网页上的按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27869225/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:23:01  来源:igfitidea点击:

python clicking a button on a webpage

pythonhtmlweb-scraping

提问by rjbogz

I currently have a script that logs me into a website and I want to have it click a button on the website if it is currently not clicked. Here is the info for the button:

我目前有一个脚本可以让我登录到一个网站,如果当前没有点击它,我想让它点击网站上的一个按钮。这是按钮的信息:

When the button is already active:

当按钮已经激活时:

<p class="toast_btn">
    <a class="button grey toast track-click active" data-user-avatar="https://dwebsite.net/picture.jpg" data-checkin-id="123456789" data-href=":feed/toast" data-track="activity_feed" href="#">

When the button is not active:

当按钮未激活时:

<p class="toast_btn">
    <a class="button grey toast track-click" data-user-avatar="https://dwebsite.net/picture.jpg" data-checkin-id="123456789" data-href=":feed/toast" data-track="activity_feed" href="#">

I am only looking to click it when class="button grey toast track-click"

我只是想在什么时候点击它 class="button grey toast track-click"

What is the best way to do this? I currently use urllib2 and mechanize to login and check a few forms currently. Thanks!

做这个的最好方式是什么?我目前使用 urllib2 和机械化登录并检查当前的一些表格。谢谢!

回答by Gabriel

When I compare the two tags I see that the difference is for the classtag. So if you can read it then you're done

当我比较这两个标签时,我发现区别在于class标签。所以如果你能阅读它,那么你就完成了

You can do it with Selenium if you like

如果你愿意,你可以用 Selenium 来做

Step 1: find the XPath - Get the XPath of the button: for that right open the page in Chrome click on it and select Inspect element - It will open the html file and right click on the highlighted line and select copy Xpath - Copy the XPath in NotePad

第 1 步:找到 XPath - 获取按钮的 XPath:为此,在 Chrome 中打开该页面,单击它并选择 Inspect element - 它将打开 html 文件并右键单击突出显示的行并选择复制 Xpath - 复制记事本中的 XPath

Now that you have the XPath you can select the button via a Python script and query the attributes

现在您有了 XPath,您可以通过 Python 脚本选择按钮并查询属性

Here is a prototype

这是一个原型

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

driver = webdriver.Firefox()
driver.get("http://www.youradress.org")#put here the adress of your page
elem = driver.find_elements_by_xpath("//*[@type='submit']")#put here the content you have put in Notepad, ie the XPath
button = driver.find_element_by_id('buttonID') //Or find button by ID.
print(elem.get_attribute("class"))
driver.close()

Hope that helps, if you have question please let me know

希望能帮到你,有问题请私信我

I used these links for documentation

我将这些链接用于文档

Python Selenium: Find object attributes using xpath

Python Selenium:使用 xpath 查找对象属性

https://selenium-python.readthedocs.io/locating-elements.html

https://selenium-python.readthedocs.io/locating-elements.html