错误消息:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//input[text()="Cancel"]"}

我也try 过使用标记名作为按钮.

用于此的HTML:

<form name="forgotPassword" id="forgotPassForm" action="https://login.salesforce.com/secur/forgotpassword.jsp"
              autocomplete="off" method="post" target="_top" novalidate="novalidate">
            <p class="username">To reset your password, enter your Salesforce username.</p>
            <input type="hidden" name="locale" value="in" />
            
            <div class="verifyform">
                <label for="un" class="label">Username</label>
                <input id="un" type="email" class="input wide mb12 mt8 username" onKeyPress="checkCaps(event)"
                       name="un" value="" autofocus />
                
                <input type="submit" name="continue" id="continue" class="button primary fiftyfifty right focus" value="Continue" />
                <input type="button" name="cancel" onclick="parent.location='/?locale=in'" class="secondary button fiftyfifty mb16"  value="Cancel" >
            </div>
            
                <input type="hidden" name="lqs" value="locale=in" />
            
            <div id='pwcaps' class='pwcaps' style='display:none'>Caps Lock is on. Please try to log in again and remember that passwords are case-sensitive.</div>
            <a class="hiddenlink" href="javascript:document.forgotPassword.submit();" id="continueReset">Continue</a>
            
                <p class="small">Video: <a href="http://salesforce.vidyard.com/watch/MxeeKTO3x5oMx4jNVWWX4w" id="video-link">Need Help Logging In?</a></p>
            
        </form>

我的代码:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By

driver = webdriver.Chrome(service=Service("C:\\Users\\Dell\\Desktop\\Selenium\\chromedriver.exe"))
driver.maximize_window()
driver.get("https://login.salesforce.com/")
driver.find_element(By.CSS_SELECTOR, '#username').send_keys('Tac')
driver.find_element(By.CSS_SELECTOR, '.password').send_keys('PASSWRd')
driver.find_element(By.CSS_SELECTOR, '.password').clear()
driver.find_element(By.LINK_TEXT, 'Forgot Your Password?').click()
driver.find_element(By.XPATH, '//button[text()="Cancel"]').click()

推荐答案

元素Cancel没有innerText,而是将100属性设置为101

<input type="button" name="cancel" onclick="parent.location='/'" class="secondary button fiftyfifty mb16" value="Cancel">

解决方案

要单击元素而不是presence_of_element_located(),您需要将WebDriverWait归纳为element_to_be_clickable(),您可以使用以下locator strategies之一:

  • 使用CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[value='Cancel']"))).click()
    
  • 使用XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@value='Cancel']"))).click()
    
  • Note:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

Python相关问答推荐

取相框中一列的第二位数字

有什么方法可以修复奇怪的y轴Python matplotlib图吗?

在后台运行的Python函数

pandas DataFrame中类型转换混乱

Python plt.text中重叠,包adjust_text不起作用,如何修复?

使用GEKKO在简单DTE系统中进行一致初始化

带条件计算最小值

' osmnx.shortest_track '返回有效源 node 和目标 node 的'无'

如何过滤包含2个指定子字符串的收件箱列名?

两个pandas的平均值按元素的结果串接元素.为什么?

我如何根据前一个连续数字改变一串数字?

当递归函数的返回值未绑定到变量时,非局部变量不更新:

Scrapy和Great Expectations(great_expectations)—不合作

合并帧,但不按合并键排序

如何找出Pandas 图中的连续空值(NaN)?

搜索按钮不工作,Python tkinter

找到相对于列表索引的当前最大值列表""

为什么调用函数的值和次数不同,递归在代码中是如何工作的?

使用Openpyxl从Excel中的折线图更改图表样式

Cython无法识别Numpy类型