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)

猜你喜欢

转载自blog.csdn.net/weixin_42480337/article/details/107542509