代码不工作。我试图用它的 ID 号单击页面上的一个元素,但我不能
Code not working. I am trying to click an element on a page with its id number but i cant
我需要使用 HTML 代码中的 ID 单击页面上的元素。此按钮的 ID 是“input-optionXXX”,其中 XXX 是 100 到 400 之间的 3 位数字。我希望通过页面上的 HTML 代码将 python 代码 运行 和找出 3 位数字(100 到 400 之间)是什么,以便它可以点击它。有帮助吗?
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)
使用 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
如果您要确定存在哪些 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)
我需要使用 HTML 代码中的 ID 单击页面上的元素。此按钮的 ID 是“input-optionXXX”,其中 XXX 是 100 到 400 之间的 3 位数字。我希望通过页面上的 HTML 代码将 python 代码 运行 和找出 3 位数字(100 到 400 之间)是什么,以便它可以点击它。有帮助吗?
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)
使用 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
如果您要确定存在哪些 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)