Python Selenium 无法点击 div/span 复选框元素

Python Selenium Unable to click on div/span checkbox element

这是我要点击的完整元素:

<div role="presentation" class="markAllContainer columnHeaderSelectAll" data-dyn-explicitcolumnwidth="custom" data-dyn-bind="
            click: $data.ToggleMarkAllRecordsMode,
            css: {
                'is-loading': $data._isLoading
             }
            ">
    <div class="columnHeaderWrapper markingColumnHeader" role="columnheader">
        <span class="marked-record-checkbox checkMarkTarget" role="checkbox" data-dyn-bind="
                    checked: $data.MarkAllRecords,
                    skip: $dyn.util.markAllSkip($data),
                    attr: {'aria-label': $dyn.label('Grid_SelectAllRowsShortcut')}" aria-checked="false" tabindex="-1" aria-label="Select all rows">
            <span class="checkMarkGlyph checkMarkTarget Checkmark-symbol"></span>
            <span class="boxGlyph checkMarkTarget"></span>
        </span>
    </div>
</div>

我尝试同时使用 ActionChainselement.click() 单击父元素以及树中的每个子元素,使用 WebDriverWait 使元素在 DOM.该元素是一个出现在页面顶部附近的复选框,因此不必滚动到它。为了测试各种点击方法,我在调试端口上打开了一个 Chrome 实例,它允许我在 selenium 中一遍又一遍地执行命令,而无需 close/reopen 浏览器。由于我要自动化的网站的性质,这也是必要的。如果有任何帮助,这在 Microsoft 的 Dynamics 365 中。

Chrome window 使用此控制台命令打开:

chrome.exe --remote-debugging-port=9222 --user-data-dir="C:\guest"

然后我在 python 中使用以下行将驱动程序连接到它:

chrome_options = Co()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
driver = Chrome(executable_path=chromedriver, options=chrome_options)

使用 ActionChains(driver).move_to_element(element).click().perform() 我得到错误 [object HTMLDivElement] has no size and location(或 HTMLSpanElement)。

使用 .click() 我只得到 element not interactable.

我也尝试过使用以下 execute_script() 命令:

driver.execute_script("arguments[0].click();", element)
driver.execute_script("arguments[0].setAttribute('aria-checked', 'true')", element)

在上面的后一个示例中,选择的元素是具有 class="marked-record-checkbox checkMarkTarget"span 子元素: element = driver.find_element_by_xpath("//span[@aria-label='Select all rows']")

然而,在这两种情况下都没有任何反应,我也没有收到任何错误消息。我可以看到这个 span 元素也是具有事件侦听器的元素。所以我很困惑为什么没有点击方法在这个元素上工作。

可以肯定的是,我当然可以手动点击这个复选框。对此的任何帮助将不胜感激,如果我可以在此处提供任何其他信息,请告诉我。我对 HTML/Selenium.

不是很有经验

编辑: 下面是我为应该相关的 span 元素复制的样式(如前所述,它具有事件侦听器):

font-style: normal;
font-variant: normal;
font-weight: 400;
border-collapse: collapse;
border-spacing: 0;
color: inherit;
user-select: none;
border: 0;
margin: 0;
padding: 0;
font-family: DynamicsSymbol;
font-size: 16px;
display: table;
width: 30px;
height: 30px;
cursor: pointer;
text-align: center;

我确实看到它没有指定“可见性”样式。这可能是原因吗?如果是这样,我将如何为样式添加“可见性”sheet?

我想通了!解决方案比我预期的要简单得多。我只需要将 xpath 查询进一步扩展到父元素。我在上面的问题中发布的元素只是复选框本身,所以我扩展了 xpath 以首先查看复选框所属的整个网格。这是对我有用的最终 xpath:

"//div[@data-dyn-controlname='Grid']/div/div/div/div/div/span[@aria-label='Select all rows']"

我假设它之前没有工作,因为 DOM 中加载了多个具有相同标签 @aria-label='Select all rows'.

的元素