Selenium / Python - 如何点击仅当光标悬停在它上面时出现的按钮?

时间:2016-02-29 09:37:38

标签: python html python-3.x selenium-webdriver

基本上在UI上有一个按钮,只有在悬停时才会显示。我需要能够在可见时单击“删除”按钮。如果有某种方式我可以通过检查编辑测试合同1:uib-tooltip="Edit Test Contract 1">

来检查我正在徘徊正确的事情,那将是理想的

Html代码:

<div class="ibox-content no-margins ">
<span tooltip-class="beacon-uib-tooltip" tooltip-append-to-body="true" uib-tooltip="Edit Test Contract 1">
<button class="name btn-link ng-binding ng-isolate-scope" ng-disabled="disableEdit()" mit-mini-modal="" ng-click="editContract(contract)">Test Contract 1</button>
</span>
<span class="icon-container hide-when-dragging">
<div class="show-when-hovering">
<span class="fa fa-times delete-icon ng-isolate-scope" uib-tooltip="Delete" tooltip-append-to-body="true" mit-mini-modal="" ng-click="deleteContract()"></span>

我遇到的python代码由于某些原因没有返回任何内容,尽管我搜索了正确的css,ng-click="deleteContract()"。我尝试单击删除按钮的方法是delete_contract()。如果您需要更多信息,请告诉我,我已尽量详细说明。

class Locator:
    def __init__(self, selenium_context, short_wait_time_sec):
        self.selenium_context = selenium_context
        self.short_wait_time_sec = short_wait_time_sec

def find_elements_css(self, cssSelector):
    return self.selenium_context.find_elements_by_css_selector(cssSelector)

def delete_contract(self, contract_name):
    self.contract_list = self.page_loc.find_elements_css('button[ng-click="deleteContract()"]')
    for contract in self.contract_list:
        if contract.text == contract_name:
            contract.click()
            break

1 个答案:

答案 0 :(得分:3)

您可以使用ActionChains来模拟悬停

def delete_contract(self, contract_name):
    action = ActionChains(selenium_context) # create ActionChains object
    contract = page_loc.find_element_by_css_selector('span[ng-click="deleteContract()"]')
    action.move_to_element(contract).perform() # move the mouse to the element
    contract.click()

删除按钮位于<span>标记中,而不是<button>标记。 cssSelctor应为'span[ng-click="deleteContract()"]'