【问题标题】:automatically click a button which pops up on webpage自动点击网页上弹出的按钮
【发布时间】:2018-06-30 15:19:45
【问题描述】:

我想自动点击网页上弹出的按钮。我怎样才能使用python做到这一点?我在 JavaScript 方面的经验为零,刚刚开始学习编程。

下面是按钮的外部 HTML:

接受

我搜索并发现了类似的问题: How to auto click an input button

Automatic click on a pop up button

请帮忙。

【问题讨论】:

标签: javascript python html automation webautomation


【解决方案1】:

对于自动化,您肯定想看看

网络机器人

它基于 selenium,并提供更多功能,只需要很少的代码,例如自动查找元素以执行单击、基于参数键入等操作。

这里是文档:https://webbot.readthedocs.io/

【讨论】:

    【解决方案2】:

    看看 Selenium http://selenium-python.readthedocs.io/

    from selenium import webdriver
    import time
    
    options = webdriver.ChromeOptions()
    options.add_argument('--ignore-certificate-errors')
    options.add_argument("--test-type")
    options.binary_location = "/usr/bin/chromium"
    driver = webdriver.Chrome(chrome_options=options)
    driver.get('http://codepad.org')
    
    # click radio button
    python_button = driver.find_elements_by_xpath("//input[@name='lang' and @value='Python']")[0]
    python_button.click()
    
    # type text
    text_area = driver.find_element_by_id('textarea')
    text_area.send_keys("print('Hello World')")
    
    # click submit button
    submit_button = driver.find_elements_by_xpath('//*[@id="editor"]/table/tbody/tr[3]/td/table/tbody/tr/td/div/table/tbody/tr/td[3]/input')[0]
    submit_button.click()
    

    (从https://pythonspot.com/selenium-click-button/盗取的代码)

    【讨论】:

    • 您好,感谢您的回复。我在 IDLE 上尝试了该程序。该网站打开,但我无法找到并正确提及 x 路径。如何获得正确的 x 路径?
    • 您需要查看 HTML 文件并从那里获取它。除了 x-path,还有其他选项可以通过 id 或 name 来定位元素。见selenium-python.readthedocs.io/locating-elements.html
    • 在火狐浏览器中可以在web开发者工具-右键元素-复制-xpath中获取xpath
    • 是的,我使用了不同的方法来定位不同的元素并且几乎完成了,但是当通过“类方法”尝试时,最后一个按钮会产生错误这是我通过类方法尝试的:give_access = driver.find_element_by_class_name("_1cPA3") then for by css 选择器:#root > div > div > div._3Vu4w._3Xzhy > div._2Jdsd > div > div._3wkMv > div > button give_access = driver.find_element_by_css_selector('#root > div > div > div._3Vu4w._3Xzhy > div._2Jdsd > div > div._3wkMv > div > button') 尝试了这个 Then for xpath:
    • 那么对于xpath: //*[@id="root"]/div/div/div[1]/div[2]/div/div[2 ]/div/button give_access = driver.find_element_by_xpath("//*[@id='root']/div/div/div[1]/div[2]/div/div[2]/div/button")[0] 但没有任何效果。 下面是我要定位的元素: give_access = driver.find_element_by_xpath("//*[ @id='root']/div/div/div[1]/div[2]/div/div[2]/div/button")[0]
    猜你喜欢
    • 2023-02-14
    • 1970-01-01
    • 1970-01-01
    • 2015-03-08
    • 2012-02-07
    • 2016-06-18
    • 2015-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多