英文:
clickable_links = driver.find_element()
问题
我试图开发一个Python代码来打印所有可点击元素的列表,但我遇到了错误。
File "C:\Users\username\PycharmProjects\Find_clickables.py", line 9, in <module>
clickable_links = driver.find_element()
^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\username\venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 739, in find_element
return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]
我尝试了以下代码 -
from selenium import webdriver
url = "http://google.com/"
driver = webdriver.Chrome()
driver.get(url)
# 找到页面上的所有可点击元素
clickable_links = driver.find_element()
for link in clickable_links:
print(link)
英文:
I was trying to develop a python code to print list of all clickable elements but I'm getting error.
File "C:\Users\username\PycharmProjects\Find_clickables.py", line 9, in <module>
clickable_links = driver.find_element()
^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\username\venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 739, in find_element
return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]
I tried below code -
from selenium import webdriver
url = "http://google.com/"
driver = webdriver.Chrome()
driver.get(url)
# Find all clickable elements on page
clickable_links = driver.find_element()
for link in clickable_links:
print(link)
答案1
得分: 0
我只列出了所有常用的 "html" 标签,这些标签是 "可交互的",还有很多其他标签,任何小部件都可以使用 JavaScript 被激活为 "按钮"。
from selenium import webdriver
from selenium.webdriver.common.by import By
# 设置webdriver(将 'driver_path' 替换为您的webdriver可执行文件的路径)
driver = webdriver.Chrome('driver_path')
# 打开一个网页
driver.get('https://www.example.com')
# 查找所有 <a> 标签
a_tags = driver.find_elements(By.TAG_NAME, 'a')
# 查找所有 <li> 标签
li_tags = driver.find_elements(By.TAG_NAME, 'li')
# 查找所有 <button> 标签
button_tags = driver.find_elements(By.TAG_NAME, 'button')
# 查找所有可点击的项目(使用CSS选择器)
clickable_items = driver.find_elements(By.CSS_SELECTOR, '[onclick], [href], [type="submit"], [type="button"], [type="image"], [role="button"]')
# 打印结果
print("找到", len(a_tags), "<a> 标签:")
for tag in a_tags:
print(tag.get_attribute('href'))
print("找到", len(li_tags), "<li> 标签:")
for tag in li_tags:
print(tag.text)
print("找到", len(button_tags), "<button> 标签:")
for tag in button_tags:
print(tag.text)
print("找到", len(clickable_items), "可点击的项目:")
for item in clickable_items:
print(item.get_attribute('onclick'), item.get_attribute('href'), item.get_attribute('type'), item.get_attribute('role'))
# 关闭webdriver
driver.quit()
英文:
I just listed all commonly used html
tags, that are interactive
, there are plenty more out there, and any widget can be stimulated as a “button” using JavaScript.
from selenium import webdriver
from selenium.webdriver.common.by import By
# Set up webdriver (replace 'driver_path' with the path to your webdriver executable)
driver = webdriver.Chrome('driver_path')
# Open a webpage
driver.get('https://www.example.com')
# Find all <a> tags
a_tags = driver.find_elements(By.TAG_NAME, 'a')
# Find all <li> tags
li_tags = driver.find_elements(By.TAG_NAME, 'li')
# Find all <button> tags
button_tags = driver.find_elements(By.TAG_NAME, 'button')
# Find all clickable items (using CSS selector)
clickable_items = driver.find_elements(By.CSS_SELECTOR, '[onclick], [href], [type="submit"], [type="button"], [type="image"], [role="button"]')
# Print the results
print("Found", len(a_tags), "<a> tags:")
for tag in a_tags:
print(tag.get_attribute('href'))
print("Found", len(li_tags), "<li> tags:")
for tag in li_tags:
print(tag.text)
print("Found", len(button_tags), "<button> tags:")
for tag in li_tags:
print(tag.text)
print("Found", len(clickable_items), "clickable items:")
for item in clickable_items:
print(item.get_attribute('onclick'), item.get_attribute('href'), item.get_attribute('type'), item.get_attribute('role'))
# Close the webdriver
driver.quit()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论