简体   繁体   English

Selenium 与 Python 3 - 如何单击元素

[英]Selenium with Python 3 - How to click on element

I am new to Selenium I have been following the documentations but they seem outdated.我是 Selenium 的新手,我一直在关注文档,但它们似乎已经过时了。

(for curious people, that is the admin section of Atlassian's Cloud https://admin.atlassian.com/s/orgID/users/userID , I wish to remove site access for multiple users) (对于好奇的人,这是 Atlassian Cloud https://admin.atlassian.com/s/orgID/users/userID的管理部分,我希望删除多个用户的站点访问权限)

Here is a subset of the HTML page:这是 HTML 页面的子集:

<label data-size="regular" class="css-ji2l50" data-checked="true">
    <input name="" type="checkbox" value="123456789abcde">
        <span role="img" aria-label="check" class="css-zg81aj">
            <svg width="24" height="24" viewBox="0 0 24 24" role="presentation">
                <path d="M7.356 10.942a.497.497 0 00-.713 0l-.7.701a.501.501 0 00-.003.71l3.706 3.707a.501.501 0 00.705.003l7.712-7.712a.493.493 0 00-.006-.708l-.7-.7a.504.504 0 00-.714 0l-6.286 6.286a.506.506 0 01-.713 0l-2.288-2.287z" fill="currentColor"></path>
            </svg>
        </span>
        <span role="img" aria-label="cross" class="css-zg81aj">
            <svg width="24" height="24" viewBox="0 0 24 24" role="presentation">
                <path d="M15.185 7.4l-3.184 3.185-3.186-3.186a.507.507 0 00-.712.003l-.7.701a.496.496 0 00-.004.712l3.185 3.184L7.4 15.185a.507.507 0 00.004.712l.7.7c.206.207.516.2.712.004l3.186-3.185 3.184 3.185a.507.507 0 00.712-.004l.701-.7a.496.496 0 00.003-.712l-3.186-3.186 3.186-3.184a.507.507 0 00-.003-.712l-.7-.7a.508.508 0 00-.36-.153.5.5 0 00-.353.15z" fill="currentColor" fill-rule="evenodd"></path>
            </svg>
        </span>
</label>

I want to capture and click on <input name="" type="checkbox" value="123456789abcde">我想捕获并点击<input name="" type="checkbox" value="123456789abcde">

Since there is no element = driver.find_element(By.VALUE,"123456789abcde") ( link to the documentation )由于没有element = driver.find_element(By.VALUE,"123456789abcde")链接到文档
I am using element = driver.find_element(By.XPATH, "//input[@value='123456789abcde']")我正在使用element = driver.find_element(By.XPATH, "//input[@value='123456789abcde']")

This seems to work fine, print(element) gives <selenium.webdriver.remote.webelement.WebElement (session="b2d92d7e20334085b7989c009fa77785", element="c23fdd99-9052-4028-9a72-e8f39873c720")>这似乎工作正常, print(element)给出<selenium.webdriver.remote.webelement.WebElement (session="b2d92d7e20334085b7989c009fa77785", element="c23fdd99-9052-4028-9a72-e8f39873c720")>

But element.click() results in a ElementClickInterceptedException exception.但是element.click()会导致 ElementClickInterceptedException 异常。 element.click does return <bound method WebElement.click of <selenium.webdriver.remote.webelement.WebElement (session="b2d92d7e20334085b7989c009fa77785", element="c23fdd99-9052-4028-9a72-e8f39873c720")>> but the button is not toggled. element.click确实返回<bound method WebElement.click of <selenium.webdriver.remote.webelement.WebElement (session="b2d92d7e20334085b7989c009fa77785", element="c23fdd99-9052-4028-9a72-e8f39873c720")>>但按钮不是切换。
在此处输入图像描述

What am I doing wrong?我究竟做错了什么? Note that I am using Edge webdriver if that matters.请注意,如果这很重要,我正在使用 Edge webdriver。

Thanks.谢谢。

EDIT: From first investigations, it seems that the the <label> is overlapping the input.编辑:从第一次调查来看,似乎<label>与输入重叠。 If I indeed click on the label itself it does toggle the button.如果我确实点击了 label 本身,它会切换按钮。 In the page code above, I tested my code on the xpath driver.find_element(By.XPATH, "//label[@data-checked='true']").click() but the thing is that there are several labels on the page that show a data-checked='true' property.在上面的页面代码中,我在 xpath driver.find_element(By.XPATH, "//label[@data-checked='true']").click()上测试了我的代码,但问题是有几个标签在显示 data-checked='true' 属性的页面上。 How to craft the xpath so that I select the label associated with that specific input.如何制作 xpath 以便 select 与该特定输入相关联的 label。 Basically mixing the "//label[@data-checked='true']" and //input[@value='123456789abcde'] xpaths.基本上混合了“//label[@data-checked='true']”和//input[@value='123456789abcde'] xpaths。

Since we have no access to that page we can only guess.由于我们无法访问该页面,我们只能猜测。
So, instead of driver.find_element(By.XPATH, "//input[@value='123456789abcde']").click() Try adding a wait.所以,而不是driver.find_element(By.XPATH, "//input[@value='123456789abcde']").click()尝试添加一个等待。 The WebdriverWait is the preferred way to do that, as following: WebdriverWait 是执行此操作的首选方式,如下所示:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 20)

wait.until(EC.element_to_be_clickable((By.XPATH, "//input[@value='123456789abcde']"))).click()

UPD UPD
In case you need to click the label element which is a direct parent of this input the locator can be updated as following:如果您需要单击作为此input的直接父元素的label元素,可以将定位器更新如下:

wait.until(EC.element_to_be_clickable((By.XPATH, "//input[@value='123456789abcde']/.."))).click()

Another way to precisely locate that label is to find the parent div based on the child input and from it to get the child label as following:另一种精确定位label的方法是根据子input找到父div ,并从中获取子label ,如下所示:

wait.until(EC.element_to_be_clickable((By.XPATH, "//div[.//input[@value='123456789abcde']]//label"))).click()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM