如何使用 Selenium 单击此按钮?

以下是我尝试根据“检查”单击的元素的所有部分:


<div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix">

    ::before

    <div class="ui-dialog-buttonset">

        <button type="button" class="done ui-button ui-corner-all ui-widget">

        Done</button>

    </div>

    ::after

</div>

当我想点击它时,我假设它在::before显示和可点击的部件上。在代码中,我确保在单击之前滚动并等待两秒钟以确保按钮可见,但我得到:


selenium.common.exceptions.ElementNotVisibleException: Message: element not interactable

但我不明白它是如何不可交互的。当我在检查器中将鼠标悬停在它上面时,所有内容都<button>...</button>突出显示,所以我想单击的按钮必须在那里,对吗?


以下是我尝试过的几件事:


browser.find_element_by_css_selector('button[type=button]').click()


browser.find_elements_by_xpath("//*[contains(text(), 'Done')]").click()


# The above returns a list for some reason?


browser.find_elements_by_css_selector('done.ui-button.ui-corner-all.ui-widget')

我希望我能记住我尝试过的一切,但无论如何我希望有人能帮助我。


天涯尽头无女友
浏览 227回答 3
3回答

繁华开满天机

当您使用.find_elements(注意复数)时,它将返回一个列表,而不是像.find_element(singular) 那样的单个元素。你有没有尝试过browser.find_element_by_xpath("//button[.='Done']")如果您接到len()这些.find_elements电话...是 1 吗?我想知道是否只有一个按钮与您使用的定位器相匹配,第一个按钮不可见,但您想要第二个或第三个,等等。

拉风的咖菲猫

您可以使用 ActionChains 移动到元素from selenium.webdriver.common.action_chains import ActionChainselement = driver.find_elements_by_css_selector("div.ui-dialog-buttonpane.ui-widget-content.ui-helper-clearfix")actions = ActionChains(driver)actions.move_to_element(element).perform()或者你可以使用 scrollIntoView() 滚动直到元素在视图中:driver.execute_script("arguments[0].scrollIntoView();", element)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python