简体   繁体   English

硒按钮单击不起作用

[英]Selenium button click not working

This is the HTML code for the source page of bitbucket.org webpage 这是bitbucket.org网页源页面的HTML代码。

    <div class="aui-layer aui-dialog2 aui-dialog2-large" role="dialog" aria-hidden="false" data-aui-focus="false" data-aui-blanketed="true" style="z-index: 3000;">  <header class="aui-dialog2-header">
    <h1 class="aui-dialog2-header-main dialog-title">Add SSH key</h1>

  </header>

<div id="bb-new-ssh-key-dialog-content" class="aui-dialog2-content "><form id="new-ssh-key" method="post" class="ssh-keys-form aui">

    <input type="hidden" name="csrfmiddlewaretoken" value="Y1fI2KoE87IKZwncZHYIh7zBpFyfXMsI">
    <div id="id_label_group" class="field-group ">
        <label for="id_label">
          Label
        </label>

          <input class=" text long-field" id="id_label" maxlength="255" name="label" type="text">
    </div>
    <div id="id_key_group" class="field-group ">
        <label for="id_key">
          Key<span class="aui-icon icon-required"></span><span class="content">required</span>
        </label>

          <textarea class=" textarea long-field" cols="40" columns="40" id="id_key" name="key" placeholder="Paste your key here..." rows="8"></textarea>
    </div>
<p class="field-group">
  <strong class="heading">Already have a key?</strong>

    Copy <a href="https://confluence.atlassian.com/x/YwV9E" target="_blank">your key</a> to your clipboard

  <span class="ssh-key-copy-help mac">with: <code>cat ~/.ssh/id_rsa.pub | pbcopy</code></span>
  <span class="ssh-key-copy-help linux" style="display: inline;">with: <code>xclip -sel clip &lt; ~/.ssh/id_rsa.pub</code></span>
</p>
<p class="field-group" id="ssh_error_help">
  <strong class="heading">Problems adding a key?</strong>

    Read our <a href="https://confluence.atlassian.com/x/2YJnJ" target="_blank">knowledge base</a> for common issues.

</p>
    <div class="buttons-container">
      <div class="buttons">
        <input type="hidden" name="action" value="add-key">
        <button type="submit" id="add_key_button" class=" hidden add_key_button">
          Add key
        </button>
      </div>
    </div>
  </form></div>

  <footer class="aui-dialog2-footer">
    <div class="aui-dialog2-footer-actions">
      <button class=" aui-button aui-button-primary dialog-submit" resolved="">
          Add key
        </button>
        <button class=" aui-button aui-button-link dialog-cancel" resolved="">Cancel</button>
    </div>

  </footer>
</div>

I want to press button with the text "Add key". 我想按下带有文本“添加键”的按钮。 I have tried following commands using selenium 我已经尝试过使用硒命令

1. driver.find_element_by_xpath("//button[contains(text(),'Add key')]").click()
2. driver.find_element_by_tag_name("footer").find_element_by_tag_name("div").find_element_by_xpath("//button[contains(text(),'Add key')]").click()
3. driver.execute_script("document.getElementByXPath(\'' + //button[contains(text(), 'Add key')] + '\').click()")

But nothing worked out of these possibilities and throws error that it is unable to locate the required element. 但是,没有任何办法解决这些可能性,并抛出错误,即无法找到所需的元素。 The window looks like a pop-up window so I have also tried by switching to the iframe, but that also failed. 该窗口看起来像一个弹出窗口,因此我也尝试过切换到iframe,但这也失败了。

在此处输入图片说明 What can I do to click this button. 我该怎么做才能单击此按钮。 Any help would be appreciated. 任何帮助,将不胜感激。

Have you tried something like: 您是否尝试过类似的方法:

 driver.find_element_by_xpath("//div[@class='aui-dialog2-footer-actions']//button[contains(text(), 'Add key')]").click()

See this post . 看到这篇文章

Be Sure that the button is visible. 确保按钮可见。 Try to use Thread.sleep(3000) or more before 尝试使用Thread.sleep(3000)或更高版本

You can try using the css selector or class name - 您可以尝试使用CSS选择器或类名-

driver.find_element_by_css_selector(".dialog-submit").click()

driver.find_element_by_class_name("dialog-submit").click()

Alternatively, you can try - 另外,您可以尝试-

wait = WebDriverWait(driver, 10)
bttn = wait.until(expectedCondition.presence_of_element_located((By.CLASS_NAME , "dialog-submit")))

driver.execute_script("arguments[0].click();", bttn)

where expectedCondition is imported as: 凡将conditionCondition导入为:

from selenium.webdriver.support import expected_conditions as expectedCondition

Hope it helps! 希望能帮助到你!

In your HTML two "add key" button are present so according to selenium feature it find first add key so you need to go on second add key button. 在您的HTML中,存在两个“添加键”按钮,因此根据硒功能,它会找到第一个添加键,因此您需要继续执行第二个添加键按钮。

For that you have use xpath 为此,您可以使用xpath

//footer[@class='aui-dialog2-footer']/div/button 

or 要么

//footer[@class='aui-dialog2-footer']/div/button//button[contains(text(),'Add key')]

Although it may proves to be a nightmare to maintain, you could try getting an absolute xpath with firebug and give it a shot. 尽管事实证明,维护它是一场噩梦,但您可以尝试使用Firebug获取绝对的xpath并尝试一下。 It looks something like "/html/body/div[1]/..." 看起来像“ / html / body / div [1] / ...”

-this would be better in a comment, but not quite there yet-. -在评论中会更好,但目前还不完全-。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM