简体   繁体   English

代码不起作用。 我试图点击页面上带有 id 号的元素,但我不能

[英]Code not working. I am trying to click an element on a page with its id number but i cant

I need to click an element on a page by using its ID from the HTML code.我需要使用 HTML 代码中的 ID 单击页面上的元素。 The ID of this button is "input-optionXXX" where XXX is a 3 digit number between 100 and 400. I want the python code to run through the HTML code on the page and find out what the 3 digit number (between 100 and 400) is so it can click on it.这个按钮的ID是“input-optionXXX”,其中XXX是100到400之间的3位数字。我想让python代码运行页面上的HTML代码,找出3位数字(100到400之间)是什么) 这样它就可以点击它了。 Any help?有什么帮助吗?

a = list(range(100,400))
for i in a:
    if EC.presence_of_element_located((By.ID, f'input-option{str(i)}')):
        driver.find_element_by_id(f'input-option{i}').click()
        print(i)
Traceback (most recent call last):
  File "C:\Users\karim\Desktop\b1.py", line 64, in <module>
    driver.find_element_by_id(f'input-option{i}').click()
  File "C:\Users\karim\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 360, in find_element_by_id
    return self.find_element(by=By.ID, value=id_)
  File "C:\Users\karim\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "C:\Users\karim\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\karim\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="input-option100"]"}
  (Session info: chrome=xxxxxxxxxxxx)

Use try...except block:使用 try...except 块:

from selenium.common.exceptions import NoSuchElementException

for i in range(100, 400):
    try:
        driver.find_element_by_id(f'input-option{i}').click()
        print(i)
    except NoSuchElementException:
        continue

If you are trying to determine which ids are present, then:如果您要确定存在哪些 ID,则:

for i in range(100, 400):
    # The following might return an empty list but should not throw an exception:
    elements = driver.find_elements_by_id(f'input-option{i}')
    if elements:
        elements[0].click()
        print(i)

暂无
暂无

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

相关问题 我正在尝试使用 iloc 为 dataframe 中的单元格分配一个值,但它不起作用。 它只是保持其原始价值 - I am trying to assign a value to a cell in a dataframe using iloc and it is not working. It is simply staying its original value 我正在尝试制作一个循环,但它不工作 - i am trying to make a loop but its not working 我试图在 if 语句中调用 function 但它不起作用。 我也在 function 中使用海龟 - I am trying to call a function inside of an if statement but it's not working. I am also using turtle in the function 我正在尝试每天在特定时间执行我的代码但它不起作用 - I am trying to execute my code at a certain time every day and its not working 我正在尝试使用 pyautogui 图像识别,我的退出代码为 0,但它不起作用 - i am trying to use pyautogui image recognition, and im coming out with a exit code of 0, but its not working 使用 Selenium 我试图单击页面上具有相同类的所有元素,但它不起作用 - Using Selenium I am trying to click all elements on the page with the same class but it is not working 我正在尝试浏览网站的页面并抓取其链接,但即使在更改页码后也会抓取相同的页面数据 - I am trying to navigate through the pages of a website and scrape its links but the same page data is scraped even after changing page number 我正在尝试在 txt 文件中导入一些文本,但它不起作用 - I am trying to import some text in a txt file but its not working 我正在尝试从 URL 获取数据,当我第一次运行代码时,它可以正常工作,但之后数据丢失了? - I am trying to fetch data from URL, When I run code 1st time its working but after it the data is missing? 我正在尝试从列表中删除所有其他元素,但它不起作用 - I am trying to delete every other element from a list, it is not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM