当前位置: 首页 > 知识库问答 >
问题:

无法使用Xpath访问Selenium Python中的按钮

罗伟志
2023-03-14

错误msg:

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

我也尝试过将标签名称作为按钮。

超文本标记语言:

<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()

共有3个答案

法玮
2023-03-14

您尝试访问的元素是输入元素Not按钮元素。

使用下面的< code>xpath来标识元素。

driver.find_element(By.XPATH, "//input[@name='cancel' and @value='Cancel']").click()

要处理动态元素,请使用WebDriver等待()并等待可单击的元素。

WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='cancel' and @value='Cancel']"))).click()

您需要导入以下库。

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
隆飞宇
2023-03-14

您是否尝试过以下代码:

driver.find_element(By.XPATH, '//*[@id="forgotPassForm"]/div[1]/input[3]').click()

要获得基本的XPath,您可以通过右键单击网页并选择“元素”来检查元素。然后,您可以右键单击该按钮,然后单击“复制”

看截图:

壤驷俊逸
2023-03-14

元素Cancel没有内部文本,而是将value属性设置为Cancel

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

要单击element而不是presence_of_element_located(),您需要为element_to_be_clickable()引入WebDriverWait,并且您可以使用以下定位器策略之一:

>

  • 使用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()
    

    注意:您必须添加以下导入:

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

  •  类似资料:
    • 我在为按钮找到正确的Xpath语法时遇到了一些困难。页面上有2个添加按钮。报告下面有一个添加按钮。项目下面有一个添加按钮。我想找到一个下面的项目,但我正在找到一个下面的报告。 我尝试了以下Xpaths,这些Xpaths在span类报告下面找到了add按钮: 我可以使用什么Xpath来定位Add...哪一个是在span类项目下面?谢了。

    • 问题的核心是,我不能刷新或更改一个场景的节点的内容(这里是TablesMain)从另一个类(这里是NamePriceCell)。 我正在使用主StackPane(TableMainController扩展StackPane)构建和应用程序,其中包含其他节点,其中一些节点是ListView。在一个特定的ListView(比如“readitemslistview”)中,我创建了一个自定义ListCel

    • 我想公开我的系统,以便通过ssh进行访问。 运行此

    • 问题内容: 尝试使用selenium-webdriver python向下滚动到页面底部,以便加载更多产品。 该网页已加载,但没有变化。 我想念什么吗? 问题答案: 您可以尝试以下move_up和move_down函数:

    • 我需要从一个包含财产细节的网站上取消信息。 “Kandy”和“Rs.3600000”的xpath表示法是什么?

    • 我正试图调查和学习更多关于库伯内特斯的知识,我遵循了指南https://blog.hazelcast.com/deploy-monitor/在我的kubernetes集群中安装hazelcast监视器。 如果我看到kubernetes仪表板,它有2个部署、3个pod和my-service为绿色。 运行此: 但当我跑的时候 它打开浏览器,如果我访问: 我有一个404。我做错了什么?