python selenium 点击按钮

2025-01-22 08:45:00
admin
原创
122
摘要:问题描述:我对 python selenium 还很陌生,我正在尝试单击具有以下 html 结构的按钮:<div class="b_div"> <div class="button c_button s_button" onclick=&qu...

问题描述:

我对 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 点击?

任何能为我指明正确方向的想法都很好。谢谢。


解决方案 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()

解决方案 2:

试试这个:

下载 Firefox,添加插件“firebug”和“firepath”;安装后,转到您的网页,启动 firebug 并找到元素的 xpath,它在页面中是唯一的,因此您不会犯任何错误。

参见图片:
操作说明

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

解决方案 3:

对于 Python,使用

from selenium.webdriver import ActionChains

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

解决方案 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,然后调用您的查找元素代码。在 stackoverflow 上查找有关此问题的更详细答案,因为我没有采用这种方法。

  2. 只需在“output_dest.txt”上找到一个类似的钩子就会产生相同的结果,这就是我所做的。

  3. 在单击任何内容之前请尝试等待一会儿:

xpath2 =“您将要点击的 xpath”

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

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

解决方案 7:

e = driver.find_element(By.XPATH, 's_image').click()

有时它不起作用!你可以尝试:

e = driver.find_element(By.XPATH, 's_image') driver.execute_script("arguments[0].click();", e)

解决方案 8:

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

  • 右键单击感兴趣的按钮并选择“检查辅助功能属性”

  • 这将打开检查器。右键单击突出显示的行,然后单击“打印到 JSON”

  • 这将打开一个新标签页。查找nodeCssSelector并复制值

这使我能够通过使用来接受 Yahoo 网站的 cookie。

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 以外的其他浏览器上运行。第二种方法应该适用于所有浏览器。

解决方案 9:

对于 Python,你可以这样做

from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()

button = driver.find_element(By.ID, "my_button")

element.send_keys(Keys.RETURN) 

这样做的原因是当您使用给webdriver.find_element定元素时,元素将被聚焦,并且当按钮被聚焦时,按“Enter”键将触发该按钮上的单击事件。

解决方案 10:

使用此代码单击按钮

# finding the button using ID
button = driver.find_element_by_id(ID)

# clicking on the button
button.click()
相关推荐
  政府信创国产化的10大政策解读一、信创国产化的背景与意义信创国产化,即信息技术应用创新国产化,是当前中国信息技术领域的一个重要发展方向。其核心在于通过自主研发和创新,实现信息技术应用的自主可控,减少对外部技术的依赖,并规避潜在的技术制裁和风险。随着全球信息技术竞争的加剧,以及某些国家对中国在科技领域的打压,信创国产化显...
工程项目管理   2941  
  为什么项目管理通常仍然耗时且低效?您是否还在反复更新电子表格、淹没在便利贴中并参加每周更新会议?这确实是耗费时间和精力。借助软件工具的帮助,您可以一目了然地全面了解您的项目。如今,国内外有足够多优秀的项目管理软件可以帮助您掌控每个项目。什么是项目管理软件?项目管理软件是广泛行业用于项目规划、资源分配和调度的软件。它使项...
项目管理软件   1803  
  PLM(产品生命周期管理)系统在企业的产品研发、生产与管理过程中扮演着至关重要的角色。然而,在实际运行中,资源冲突是经常会遇到的难题。资源冲突可能导致项目进度延迟、成本增加以及产品质量下降等一系列问题,严重影响企业的效益与竞争力。因此,如何有效应对PLM系统中的资源冲突,成为众多企业关注的焦点。接下来,我们将详细探讨5...
plm项目管理系统   31  
  敏捷项目管理与产品生命周期管理(PLM)的融合,正成为企业在复杂多变的市场环境中提升研发效率、增强竞争力的关键举措。随着技术的飞速发展和市场需求的快速更迭,传统的研发流程面临着诸多挑战,而将敏捷项目管理理念融入PLM,有望在2025年实现研发流程的深度优化,为企业创造更大的价值。理解敏捷项目管理与PLM的核心概念敏捷项...
plm项目   31  
  模块化设计在现代产品开发中扮演着至关重要的角色,它能够提升产品开发效率、降低成本、增强产品的可维护性与可扩展性。而产品生命周期管理(PLM)系统作为整合产品全生命周期信息的关键平台,对模块化设计有着强大的支持能力。随着技术的不断发展,到 2025 年,PLM 系统在支持模块化设计方面将有一系列令人瞩目的技术实践。数字化...
plm软件   28  
热门文章
项目管理软件有哪些?
曾咪二维码

扫码咨询,免费领取项目管理大礼包!

云禅道AD
禅道项目管理软件

云端的项目管理软件

尊享禅道项目软件收费版功能

无需维护,随时随地协同办公

内置subversion和git源码管理

每天备份,随时转为私有部署

免费试用