selenium关于form表单的提交问题无法跳转的问题,或者点击无效的问题_江建武的博客-程序员秘密

说来惭愧,这个问题耽误我3个小时,最后去国外网站找到答案。

踩过的坑有:

from selenium.webdriver.common.keys import Keys
# js = 'document.getElementById("login_btn").click();'
# driver.execute_script(js)
# driver.excute_script('document.getElementById(" ").click()')
# driver.find_element_by_id("login_btn").click()
# print(driver.find_element_by_id("login_btn"))
# print(driver.find_element_by_xpath("//*[@id='login_btn']").click())
# driver.find_element_by_xpath("//*[@id='login_btn']")

# # #用element.send_keys(Keys.ENTER)代替element.click()就行了,亲测有效!
# driver.find_element_by_xpath("//*[@id='login_btn']").send_keys(Keys.Space)

以上答案都不对啊。完全耽误事情。

国外网站的案例:传送门 https://stackoverflow.com/questions/56604119/selecting-a-submit-button-with-python-selenium

    {%for p in product %}
 <div class="single-product">
    <div class="product-f-image">
      <img src="https://img-blog.csdnimg.cn/2022010613462655405.png" alt="">
         <div class="">
       {% if not user.is_authenticated %}
      <form action="/login/">
       <button class="add-to-cart-link" type="submit"> Add to cart</button>
      </form>
      {%else%}
    <form id="form-id"  action="" method="POST">
    {% csrf_token %}
  <button class="add-to-cart-link" type="submit" name="product" value="{
  {p.id}}" >
<input type="hidden" name="product_name" value="{
  {p.name}}">
<input type="hidden" name="product_price" value="{
  {p.lst_price}}">
    Add to cart</button>
  </form>
     {%endif%}
 <a href="#" class="view-details-link"><i class="fa fa-link"></i> See details</a>
 </div>
  </div>
 <h2><a href="single-product.html">{
  {p.id}}</a></h2>
<div class="product-carousel-price">
 <ins>{
  {p.lst_price}} €</ins>
                                </div>

                            </div>
                            {%endfor%}

他的解决方法有:

 

bon_commande = self.selenium.find_element_by_xpath("//button[@name='product' and @value='37']/parent::form")
bon_commande.submit()

You don't need to locate Submit button to submit a form - use any element inside form or form element itself:

self.selenium.find_element_by_id("form-id").submit()

self.selenium.find_element_by_class_name("add-to-cart-link").submit()

To click on the button with text as Add to cart you can use the following line of code :

self.selenium.find_element_by_xpath("//form[@id='form-id']/button[@class='add-to-cart-link' and @name='product']").submit()
#or
self.selenium.find_element_by_xpath("//form[@id='form-id']/button[@class='add-to-cart-link' and @name='product']").click()

我要实现自动登录卖家精灵该网页的源代码:

                    <form id="form_signin" action='/w/user/signin' method="post">
                        <input type="hidden" name="callback" value="">
                        <div class="login-content">
                        <input type="hidden" name="password"  value="">
                                <div class="input-group u-form" style="margin-top: 5rem">
                                    <input type="text" name="email" value=""
                                           class="form-control form-control-sm u-form__input" maxlength="50"
                                           placeholder="手机号/邮箱/子账号"
                                           pattern="^(1[3|4|5|6|7|8|9]\d{9})|([\w\-\.]{2,30}@[0-9A-Za-z\-]{1,}\.[a-zA-Z]{2,}(\.[a-zA-Z]{2,})?)|([\w|-]*)$"
                                           required autofocus>
                                </div>
                                <div class="input-group u-form mt-3">
                                    <input type="password" name="password_otn" class="form-control form-control-sm u-form__input"
                                           placeholder="密 码" required>
                                </div>
                                <div class="mt-3">
                                    <a class="small u-link-ss-grey" href="/cn/help/child-account-login" target="_blank">子账号无法登录?</a>
                                    <a class="small u-link-ss-grey pull-right" href="/cn/w/user/forgot-choose">找回密码</a>
                                </div>
                                <p class="text-danger m-0 mt-3 small" id="login_error_message"></p>
                                <button class="btn btn-sm u-btn-orange transition-3d-hover w-100 mt-5 login-btn" type="submit" id="login_btn">
                                    立即登录
                                </button>
                                <div class="text-center small mt-3 mb-5">
                                    <a class="u-link-ss" href="/cn/w/user/signup">立即注册</a>
                                    <span class="text-muted">&nbsp;|&nbsp;</span>
                                    <a class="u-link-ss-grey" href="/cn">返回首页</a>
                                </div>
                            </div>

 

最后放出我想要登录卖家精灵做的代码:

 

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


username="1770218817x"
password="规划法国恢复规划xxxx"
driver = webdriver.Chrome(r"E:\chromedriver.exe")  # Optional argument, if not specified will search path.
driver.get('https://www.sellersprite.com/cn/w/user/login#pills-account')

driver.find_element_by_xpath("//*[@id='pills-login']/li[2]/a").click()
time.sleep(1)
user = driver.find_element_by_xpath("//*[@id='form_signin']/div/div[1]/input")
user.send_keys(username)
# 找到密码输入密码
driver.find_element_by_xpath("//*[@id='form_signin']/div/div[2]/input").send_keys(password)
# 点击登录按钮实现登录
time.sleep(4)

driver.find_element_by_xpath("//*[@id='form_signin']//button[@id='login_btn']").click()


time.sleep(3)
# 点击进入发帖页面
# drive.switch_to_default_content()
cookies_list = driver.get_cookies()
print(cookies_list)

 

 

 

 

 

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_42480337/article/details/107542509

智能推荐

iPhoneX适配之UI设计、交互设计_ui设计 iphonex状态栏源文件_^_^!的博客-程序员秘密

http://blog.csdn.net/u011363981/article/details/78011757前言        苹果近日发布了全新的iPhone X全面屏手机,这也是首款采用OLED屏幕的iPhone手机,它的正面设计被更多人吐槽,之前被戏称为“刘海”的凹槽现在有了更多其他名称,“眉毛”、“头帘”等等。对于用户而言,iPhone X 的刘海可

Android调试工具 MAT _幸福过饱和的博客-程序员秘密

转自:http://univasity.iteye.com/blog/1105619简介: Eclipse提供的一个内存分析工具。它是一个功能丰富的 JAVA 堆转储文件分析工具,可以帮助你发现内存漏洞和减少内存消耗。官网地址:http://www.eclipse.org/mat 安装 (如果你使用的是MOTODEV Studio for Android则默认自带了该工具) 你可以选择安装E...

破解某Wifi APP,无需Root也可查看密码_手机通过抓包破wifi密码_XXOOYC的博客-程序员秘密

这款蹭网神器也是用了一段时间了,遗憾的就是不能看到别人的Wifi密码。虽说小米手机可以扫描二维码看到wifi密码,但每次都这么操作还是比较蛋疼。于是想能不能让软件自己主动把密码显示出来。从官网上下载最新版本:4.1.3第一步:破解签名保护重新打包,发现它有签名保护,非官方签名的话不会获取到周围的wifi热点密码。那么就需要先破它的签名保护,这是在libw

Excel资源收集_excelvba青牛在线_gCode Teacher 格码致知的博客-程序员秘密

a.. http://www.excelhome.net  国内优秀的Excel专业站点,有着海量的贴子、文章和应用资源,论坛上活跃着大量的MS MVP和Excel技术高手。站点内容和活动丰富多彩,近几年出版的一系列Excel图书广 受读者好评。  b.. h

Java 开发必会的 Linux 命令_xls51314202005的博客-程序员秘密

Java 开发必会的 Linux 命令oschina 发布于: 2015年12月17日 (7评)分享到: 收藏 +165开源中国众包平台 —— 只为自己编码 » 作为一个Java开发人员,有些常用的Linux命令必须掌握。即时平时开发过程中不使用Linux(Unix)或者mac系统,也需要熟练掌握Linux命令。因为很多服务器上都是Linux系统。所以,要和服务器机器...

怎么用matlab做loop_用小程序做微信卖菜社区团购,可行吗?怎么做?_weixin_39903538的博客-程序员秘密

自诞生以来,微信小程序因为具有获客成本低、使用便捷等优势,获得了许多商家及用户的青睐,例如最近很火爆的本身体量较轻的“社区团购”行业,包括兴盛优选、食享会、十荟团、考拉精选、同程生活精选、美家买菜、小步优鲜、谊品到家等头部平台,也都是依赖于微信小程序,面向居民社区做卖菜卖水果的生意。许多商家想要通过开发社区团购小程序,来开展微信卖菜团购。那么用小程序做微信卖菜社区团购怎么样呢?今天小编就为大家解答...

随便推点

python添加包路径_怎么找python包的路径_weixin_39612540的博客-程序员秘密

Python是如何寻找包的现在大家的电脑上很可能不只有一个Python,还有更多的虚拟环境,导致安装包的时候,一不小心你就忘记注意安装包的路径了。首先我们来解决找包的问题,这个问题回答起来很简单,但很多人不知道这个原理。假如你的Python解释器的路径是/bin/python,那么你启动Python交互环境或者用这个解释器运行脚本时,会默认寻找以下位置1:/lib(标准库路径)/lib/pytho...

Axis2与CXF的区别_axis2和cxf_kaixin201505的博客-程序员秘密

对比Axis2和CXFjws的发布对java webservice框架产生了巨大的影响,经过大浪淘沙,目前java开发webservice的框架主要包括axis2和cxf。axis2和cxf都是apache旗下的产品,但是其目的不同,导致webservice开发方法也不一样。两个框架都得到了开发者的支持。比较这两个框架的WebService开发方法与比较它们的特性同样重要。从开发者的角度,两个框架的特性相当的不同。Axis2的开发方式类似一个小型的应用服务器,Axis2的开发包要以WAR的...

Linux串口编程 —— 4G模块短信获取与删除_linux读取短信_梦小羊的博客-程序员秘密

文章目录一. 回显短信的两种方式1.1 PDU编码显示1.2 Unicode显示二. 相关指令的学习2.1 AT+CMGF 设置短信回显模式2.2 AT+CMGL 查看短信2.2.1 AT+CMGF=0 模式2.2.2 AT+CMGF=1 模式2.3 AT+CMGD 删除短信三. 编程实现短信的获取与解析3.1 字符流转字节流3.2 利用iconv系列函数完成转码3.3 上层调用,获取指定号码的首条短信3.4 删除指定号码的首条短信四. 测试前面在座的项目一直在短信的发送上下功夫,尤其是在PDU编码上花了

修改 vscode 中主题注释中文的斜体格式_vscode斜体_毛姆的酒馆的博客-程序员秘密

方法一:直接在vscode 中的 setting 文件进行更改,但是对有些主题不起作用"editor.tokenColorCustomizations": { "comments": { // 设置字体样式 加粗 下划线 斜体等 "fontStyle": "", // bold italic underline // 设置字体颜色 // "foreground": "#4caee2" "fo

QObject::No such slot 接收者::槽函数名(参数)或QObject::connect: No such signal 发送者::信号名(参数)_嵌入式-小王的博客-程序员秘密

在Qt中使用带参数的自定义槽函数进行父子窗口通信时,遇到了如下错误提示:QObject::connect: No such signal Form::sendListGot(const QString &amp;list) in ***只是因为在编写自定义信号和自定义槽函数时,虽然信号和槽都是带参数的,但是使用connect进行连接时,只能在函数中填写参数的数据类型而不能将形参名列出正...

AlphaGo Zero论文中文版:Mastering the game of Go without human knowledge_啊哈2020的博客-程序员秘密

转自:http://blog.csdn.net/lpjishu/article/details/78291152题目(Nature论文)Mastering the game of Go without human knowledge作者David Silver1*, Julian Schrittwieser1*, Karen Simonyan1*, Ioannis Antonoglou1, Aja...

推荐文章

热门文章

相关标签