1

This is the HTML code for the source page of bitbucket.org webpage

    <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.

enter image description here What can I do to click this button. Any help would be appreciated.

Vivek Puri
  • 161
  • 1
  • 1
  • 13
  • Are you logging into bitbucket in your actual code? The `driver.find_element_by_xpath("//button[contains(text(),'Add key')]").click()` actually looks good to me. Have you tried to add a `time.sleep()` before it (just for debugging purposes)? Thanks. – alecxe Feb 25 '16 at 05:51
  • Yes, I have added `time.sleep(10)` for testing purpose. – Vivek Puri Feb 25 '16 at 06:00

5 Answers5

2

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.

Community
  • 1
  • 1
rrw
  • 671
  • 7
  • 18
  • This throws an error `Message: Element is not clickable at point (579, 257). Other element would receive the click: ` – Vivek Puri Feb 25 '16 at 05:45
  • I dont understand why errs like that. Is the button visible? and why do you have 2 buttons (Add key) on your html? – rrw Feb 25 '16 at 06:00
  • I also don't know why is it so. – Vivek Puri Feb 25 '16 at 06:03
  • is the button visible during the element.click() ?? – rrw Feb 25 '16 at 06:07
  • I have added `driver.find_element_by_xpath("(//textarea[@name='key'])[2]").send_keys('key added')` for sending keys to the input area. This is working fine but I am not sure about this because it is throwing error that it is not clickable so thats why I posted my querry here. – Vivek Puri Feb 25 '16 at 06:10
  • Only 2 possible reason are that the button is not visible or the page has been refreshed or unloaded before the button click was even executed. Can you confirm that these event havent occured? By the way what is the error message of your first implementations? – rrw Feb 25 '16 at 06:16
  • `1 . driver.find_element_by_xpath("(//textarea[@name='key'])[2]").send_keys('key added')` `2. Unable to locate element` I am working on this from last 3 days. I am irritated with this error. Please help how can I make it clickable. You can visit bitbucket.org and yourself can Identify the problem. Please help with this. – Vivek Puri Feb 25 '16 at 06:20
  • Did you try to add wait visibility of element before the click function, like WebElement element = wait.until( ExpectedConditions.visibilityOfElementLocated(By.xpath(xxxx))); – aldrien.h Feb 25 '16 at 06:28
  • Message: Element is not currently visible and so may not be interacted with – Vivek Puri Feb 25 '16 at 06:53
  • Sorry, it was the wrong div, please try `driver.find_element_by_xpath("//div[@class='aui-dialog2-footer-actions']//button[contains(text(), 'Add key')]").click()` – rrw Feb 25 '16 at 07:13
  • selenium.common.exceptions.InvalidSelectorException – Vivek Puri Feb 25 '16 at 09:20
0

Be Sure that the button is visible. Try to use Thread.sleep(3000) or more before

Fazz
  • 101
  • 10
  • See, then its not related to time related problem. It seems like in the code you are using the text "Add Key" several times and the path is getting confuse, which to locate..so better to use some id or any different case instead of text as Add key. – Fazz Feb 25 '16 at 06:25
0

You can try using the css selector or class name -

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:

from selenium.webdriver.support import expected_conditions as expectedCondition

Hope it helps!

Sanchita
  • 84
  • 3
0

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.

For that you have use xpath

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

or

//footer[@class='aui-dialog2-footer']/div/button//button[contains(text(),'Add key')]
Kuldeep Yadav
  • 107
  • 1
  • 14
0

Although it may proves to be a nightmare to maintain, you could try getting an absolute xpath with firebug and give it a shot. It looks something like "/html/body/div[1]/..."

-this would be better in a comment, but not quite there yet-.