【问题标题】:python selenium click on buttonpython selenium点击按钮
【发布时间】:2014-02-16 12:08:13
【问题描述】:

我对 python selenium 很陌生,我正在尝试单击具有以下 html 结构的按钮:

<div class="b_div">

    <div class="button c_button s_button" onclick="submitForm('mTF')">
        <input class="very_small" type="button"></input>
        <div class="s_image"></div>
        <span>
           Search
        </span>
    </div>

    <div class="button c_button s_button" onclick="submitForm('rMTF')" style="margin-bottom: 30px;">
        <input class="v_small" type="button"></input>
        <span>
              Reset
        </span>
   </div>

</div>

我希望能够同时点击上面的SearchReset 按钮(显然是单独的)。

我已经尝试了几件事,例如:

driver.find_element_by_css_selector('.button .c_button .s_button').click()

或者,

driver.find_element_by_name('s_image').click()

或者,

driver.find_element_by_class_name('s_image').click()

但是,我似乎总是以NoSuchElementException 结尾,例如:

selenium.common.exceptions.NoSuchElementException: Message: u'Unable to locate element: {"method":"name","selector":"s_image"}' ;

我想知道是否可以以某种方式使用 HTML 的 onclick 属性来使 selenium 点击?

任何可以为我指明正确方向的想法都会很棒。 谢谢。

【问题讨论】:

    标签: python selenium selenium-webdriver onclick click


    【解决方案1】:

    删除css选择器中类之间的空格:

    driver.find_element_by_css_selector('.button .c_button .s_button').click()
    #                                           ^         ^
    

    =>

    driver.find_element_by_css_selector('.button.c_button.s_button').click()
    

    【讨论】:

    • 我已经尝试了您的建议。我得到同样的NoSuchElementException 错误!
    • @AJW,尝试print(driver.page_source),并检查 html 是否实际包含该元素。
    • 谢谢。我做了print(driver.page_source),发现它的名字不同。奇怪的。当我把空间拿走并重命名时,它现在点击了。在后续行动中:正如您所见,即使重置按钮和搜索按钮也具有相同的class:在这种情况下单击时如何区分搜索按钮和重置按钮?
    • @AJW,如何使用 xpath:driver.find_element_by_xpath('.//div[@class="button c_button s_button"][contains(., "Search")]')
    • @MortezaLSC,如果你的意思是在没有 GUI 的系统中是可能的,那么它是可能的。使用无头浏览器。例如,PhantomJS。
    【解决方案2】:

    试试这个:

    下载firefox,添加插件“firebug”和“firepath”;安装后进入你的网页,启动firebug并找到元素的xpath,它在页面中是唯一的,所以你不会出错。

    看图:

    browser.find_element_by_xpath('just copy and paste the Xpath').click()

    【讨论】:

    • 非常感谢您提供这么棒的生活小窍门。它节省了很多小时
    • 这在 mac bc 上不起作用吗?firebug 和 fire path 都没有显示为附加组件
    • 有时候不是操作系统的问题,而是火狐版本的问题,上一个火狐版本的FirePath有问题,我用的是火狐55.0.3
    • 你可以在 Firefox 上使用:工具->Web Developer->Inspector 找到该元素;单击 GUI 上的按钮,在检查器部分,用鼠标右键单击相关代码-> 复制并选择:CSS Selector / CSS Path / Xpath ...
    • 为我节省了几个小时......谢谢伙计......现在你不需要萤火虫,只需要 xPath 就可以在这里找到 xPath 插件:addons.mozilla.org/en-US/firefox/addon/xpath_finder
    【解决方案3】:

    对于python,使用

    from selenium.webdriver import ActionChains
    

    ActionChains(browser).click(element).perform()
    

    【讨论】:

    • 什么是element
    • @rosefun element 可以指:&lt;h1&gt;My First Heading&lt;/h1&gt;&lt;p&gt;My first paragraph.&lt;/p&gt;&lt;button&gt;Submit&lt;/button&gt;,或者任何介于开始标签和结束标签之间的东西
    【解决方案4】:

    打开一个网站https://adviserinfo.sec.gov/compilation 并单击按钮下载文件,如果它使用 python selenium,我什至想关闭弹出窗口

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    import time
    from selenium.webdriver.chrome.options import Options 
    
    #For Mac - If you use windows change the chromedriver location
    chrome_path = '/usr/local/bin/chromedriver'
    driver = webdriver.Chrome(chrome_path)
    
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument("--disable-popup-blocking")
    
    driver.maximize_window()
    driver.get("https://adviserinfo.sec.gov/compilation")
    
    # driver.get("https://adviserinfo.sec.gov/")
    # tabName = driver.find_element_by_link_text("Investment Adviser Data")
    # tabName.click()
    
    time.sleep(3)
    
    # report1 = driver.find_element_by_xpath("//div[@class='compilation-container ng-scope layout-column flex']//div[1]//div[1]//div[1]//div[2]//button[1]")
    
    report1 = driver.find_element_by_xpath("//button[@analytics-label='IAPD - SEC Investment Adviser Report (GZIP)']")
    
    # print(report1)
    report1.click()
    
    time.sleep(5)
    
    driver.close()
    

    【讨论】:

      【解决方案5】:

      我在使用 Phantomjs 作为浏览器时遇到了同样的问题,所以我通过以下方式解决了:

      driver.find_element_by_css_selector('div.button.c_button.s_button').click()
      

      基本上我已将 DIV 标记的名称添加到引号中。

      【讨论】:

        【解决方案6】:

        以下调试过程帮助我解决了类似的问题。

        with open("output_init.txt", "w") as text_file:
            text_file.write(driver.page_source.encode('ascii','ignore'))
        
        
        xpath1 = "the xpath of the link you want to click on"
        destination_page_link = driver.find_element_by_xpath(xpath1)
        destination_page_link.click()
        
        
        with open("output_dest.txt", "w") as text_file:
            text_file.write(driver.page_source.encode('ascii','ignore'))
        

        然后您应该有两个文本文件,其中包含您所在的初始页面('output_init.txt')和单击按钮后被转发到的页面('output_dest.txt')。如果它们相同,那么是的,您的代码不起作用。如果不是,那么您的代码有效,但您还有另一个问题。 对我来说,问题似乎是转换内容以生成我的钩子的必要 javascript 尚未执行。

        我认为你的选择:

        1. 让驱动程序执行 javascript,然后调用你的 find 元素代码。在此寻找更详细的答案 stackoverflow,因为我没有遵循这种方法。
        2. 只需在“output_dest.txt”上找到一个类似的钩子,它会产生相同的结果,这就是我所做的。
        3. 尝试在点击任何内容之前稍等片刻:

        xpath2 = "你要点击的 xpath"

        WebDriverWait(驱动程序,超时=5).until(lambda x: x.find_element_by_xpath(xpath2))

        xpath 方法不一定更好,我只是喜欢它,你也可以使用你的选择器方法。

        【讨论】:

          【解决方案7】:

          我遇到了同样的问题,使用 Firefox,我通过以下步骤获得了按钮元素:

          • 右键单击感兴趣的按钮并选择“检查辅助功能属性”
          • 这会打开检查器。右键单击突出显示的行,然后单击“打印到 JSON”
          • 这将打开一个新选项卡。查找nodeCssSelector 并复制该值

          这允许我接受雅虎网站的cookies。

          url = "https://yahoo.com"
          driver = Firefox(executable_path="geckodriver.exe")
          driver.get(url)
          driver.find_element_by_css_selector("button.btn:nth-child(5)").click()
          

          我对此进行了进一步测试,它使我能够轻松地接受单个 cookie。只需重复前面提到的步骤即可获取按钮名称。

          url = "https://yahoo.com"
          driver = Firefox(executable_path="geckodriver.exe")
          driver.get(url)
          driver.find_element_by_css_selector("a.btn").click()
          driver.find_element_by_css_selector(".firstPartyAds > div:nth-child(2) > label:nth-child(1)").click()
          driver.find_element_by_css_selector(".preciseGeolocation > div:nth-child(2) > label:nth-child(1)").click()
          driver.find_element_by_css_selector("button.btn").click()
          

          另一种方法是

          • 右键单击感兴趣的按钮并选择“检查”
          • 右键单击突出显示的行,然后单击“复制 -> CSS 选择器”或您需要的任何内容(有多个选项,包括 XPath)

          但是,我认为第二种方法可能包含空格,具体取决于您复制的内容,因此您可能需要手动删除(部分)空格。第一种方法似乎更万无一失,但我不知道它是否/如何在 Firefox 以外的其他浏览器上工作。第二种方法应该适用于所有浏览器。

          【讨论】:

            猜你喜欢
            • 2016-05-08
            • 2021-09-14
            • 2018-05-11
            • 1970-01-01
            • 2021-11-06
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多