python点击网页上的按钮 [英] python clicking a button on a webpage

查看:160
本文介绍了python点击网页上的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



当按钮已经激活时:

 < p class =toast_btn> 

当按钮没有激活时:

 < p class =toast_btn> 

我只希望在 class =button grey toast track -click



执行此操作的最佳方法是什么?我目前使用urllib2并机械化登录并检查几个表单。感谢!

解决方案

当我比较两个标签时,我发现差别在于 class 标记。
所以如果你可以阅读它,那么你就完成了。如果你喜欢,你可以用Selenium来完成。



第1步:找到XPath
- 获取按钮的XPath:为此右键打开Chrome页面,点击它并选择Inspect元素
- 它将打开html文件并右键点击突出显示的行并选择copy Xpath
- 在NotePad中复制XPath

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



这是一个原型

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

driver = webdriver.Firefox()
driver.get(http://www.youradress.org)#在这里放置你的页面的地址
elem = driver.find_elements_by_xpath(// * [@ type ='submit'])#把这些内容放到记事本中,例如XPath
print (elem .get_attribute(class))
driver.close()

H如果你有问题,请让我知道



我使用这些链接进行文档编制。





http://selenium-python.readthedocs .org / en / latest / locating-elements.html


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"

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

解决方案

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

You can do it with Selenium if you like

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

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

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
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

http://selenium-python.readthedocs.org/en/latest/locating-elements.html

这篇关于python点击网页上的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆