【问题标题】:How do I find an element by its id in Selenium?如何在 Selenium 中通过 id 找到元素?
【发布时间】:2019-11-23 18:23:47
【问题描述】:

这是我的代码:

我使用了 ID RESULT_RadioButton-7_0 的查找元素,但出现以下错误:

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome(executable_path="/home/real/Desktop/Selenium_with_python/SeleniumProjects/chromedriver_linux64/chromedriver")

driver.get("https://fs2.formsite.com/meherpavan/form2/index.html?153770259640")


radiostatus = driver.find_element(By.ID, "RESULT_RadioButton-7_0").click()

【问题讨论】:

  • 请分享您遇到的实际错误。
  • 您没有发布错误,但如果问题是 radiostatusNone,那是因为您将其分配给了 click() 返回值,而不是 find_element
  • 我的错误是这样的:lementClickInterceptedException:元素点击被拦截:元素 在点 (40, 567) 不可点击。其他元素会收到点击:(会话信息:chrome=78.0.3904.70)

标签: python selenium selenium-webdriver


【解决方案1】:

请找到以下答案,这将帮助您从链接中单击男性单选按钮。

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

driver = webdriver.Chrome(executable_path=r"C:\New folder\chromedriver.exe")
driver.maximize_window()
driver.get('https://fs2.formsite.com/meherpavan/form2/index.html?153770259640')

# clicking on the male checkbox button

maleRadioButton = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "RESULT_RadioButton-7_0")))
ActionChains(driver).move_to_element(maleRadioButton).click().perform()

【讨论】:

  • 为什么要使用操作类来执行常规点击?用户没有说点击问题,他们只是想定位元素。
  • 我已经尝试过您的代码,但我面临以下错误,以避免我提供了另一种解决方案:selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element 在点 (355, 606) 不可点击。其他元素会收到点击:(会话信息:chrome=78.0.3904.108)
  • 值得澄清的是,在您发布的答案中,其他看到您的答案的用户会理解您为什么使用动作链类。
  • 下次会注意的,我现在可以看到您已经更新了代码来处理 ElementClickIntercepted 异常,谢谢
【解决方案2】:

根据您提供的页面链接,您的定位器策略在这里看起来是正确的。如果您遇到错误——很可能是NoSuchElementException,我假设它可能与在尝试查找元素之前等待页面加载有关。让我们使用ExpectedConditions 类来等待元素存在,然后再定位它:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# add the above references to your .py file

# wait on element to exist, store its reference in radiostatus
radiostatus = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "RESULT_RadioButton-7_0")))

# click the element
#radiostatus.click()

# click intercepted workaround: javascript click
driver.execute_script("arguments[0].click();", radiostatus)

这将勾选表单上“男性”旁边的单选按钮。

希望这能解决您的错误。

【讨论】:

    【解决方案3】:

    除非您需要等待元素(这似乎没有必要),否则您应该能够执行以下操作:

    element_to_click_or_whatever = driver.find_element_by_id('RESULT_RadioButton-7_0')
    

    如果您查看find_element_by_id 的来源,它会以By.ID 作为参数调用find_element

    def find_element_by_id(self, id_):
        return self.find_element(by=By.ID, value=id_)
    

    IMO:find_element_by_id 读起来更好,而且要导入的包更少。

    编辑:

    我不认为你的问题是找到元素;尝试单击元素时出现ElementClickInterceptedException。例如,单选按钮已定位,但(奇怪的是)selenium 认为它没有显示。

    from selenium import webdriver
    
    driver = webdriver.Chrome()
    driver.maximize_window()
    
    driver.get("https://fs2.formsite.com/meherpavan/form2/index.html?153770259640")
    
    radiostatus = driver.find_element_by_id('RESULT_RadioButton-7_0')
    
    if radiostatus:
        print('found')
        # found
        print(radiostatus.is_displayed())
        # False
    

    【讨论】:

    • 如果find_element_by_id 与使用find_element(By.ID) 相同,那么这个答案与他们的示例中发布的原始问题有何不同?
    猜你喜欢
    • 1970-01-01
    • 2019-05-22
    • 2021-10-18
    • 2021-06-22
    • 1970-01-01
    • 2017-07-23
    • 1970-01-01
    • 2014-11-12
    • 1970-01-01
    相关资源
    最近更新 更多