2016-02-25 138 views
1

这是bitbucket.org网页的源页面硒按一下按钮不起作用

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

我想按按钮文本“添加键”的HTML代码。我一直在使用硒

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

尝试下面的命令,但没有奏效了这些可能性,并引发错误,它无法找到所需要的元素。该窗口看起来像一个弹出窗口,所以我也尝试切换到iframe,但也失败了。

enter image description here 如何才能点击此按钮。任何帮助,将不胜感激。

+0

你是否在你的实际代码中登录到bitbucket? 'driver.find_element_by_xpath(“// button [contains(text(),'Add key')]”)。click()'实际上对我来说看起来不错。你有没有尝试在它之前添加'time.sleep()'(仅用于调试目的)?谢谢。 – alecxe

+0

是的,我添加了'time.sleep(10)'用于测试目的。 –

回答

2

你有没有尝试过这样的:

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

this post

+0

这会引发错误消息:元素在点(579,257)处不可点击。其他元素会收到点击:' –

+0

我不明白为什么这样的错误。按钮是否可见? 和为什么你有2个按钮(添加键)在您的HTML? – rrw

+0

我也不知道为什么会这样。 –

0

确定该按钮是可见的。尝试使用了Thread.sleep(3000)以上之前

+0

我已经使用time.sleep(10)但它没有工作 –

+0

请参阅,那么它与时间相关的问题没有关系。它似乎在代码中使用文本“添加密钥”多次,路径变得混乱,找到..更好地使用一些id或任何不同的情况下,而不是文本作为添加密钥。 – Fazz

0

您可以尝试使用的CSS选择器或类名称 -

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

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

或者,你可以尝试 -

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

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

其中expectedCondition是进口为:

from selenium.webdriver.support import expected_conditions as expectedCondition 

希望它有帮助!

0

在你的HTML中有两个“添加键”按钮,所以根据硒功能它找到第一个添加键,所以你需要继续第二个添加键按钮。

对于您有使用XPath的

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

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

虽然它可能被证明是一场噩梦维护,你可以尝试获得与萤火虫的绝对XPath,并给它一个镜头。它看起来像“/ html/body/div [1]/...”

- 这在评论中会更好,但还不是那么完美。