clickable_links = driver.find_element()

huangapple go评论108阅读模式
英文:

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 &quot;C:\Users\username\PycharmProjects\Find_clickables.py&quot;, line 9, in &lt;module&gt;
    clickable_links = driver.find_element()
                      ^^^^^^^^^^^^^^^^^^^^^
  File &quot;C:\Users\username\venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py&quot;, line 739, in find_element
    return self.execute(Command.FIND_ELEMENT, {&quot;using&quot;: by, &quot;value&quot;: value})[&quot;value&quot;]

I tried below code -

from selenium import webdriver
url = &quot;http://google.com/&quot;
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 &#39;driver_path&#39; with the path to your webdriver executable)
driver = webdriver.Chrome(&#39;driver_path&#39;)

# Open a webpage
driver.get(&#39;https://www.example.com&#39;)

# Find all &lt;a&gt; tags
a_tags = driver.find_elements(By.TAG_NAME, &#39;a&#39;)

# Find all &lt;li&gt; tags
li_tags = driver.find_elements(By.TAG_NAME, &#39;li&#39;)

# Find all &lt;button&gt; tags
button_tags = driver.find_elements(By.TAG_NAME, &#39;button&#39;)

# Find all clickable items (using CSS selector)
clickable_items = driver.find_elements(By.CSS_SELECTOR, &#39;[onclick], [href], [type=&quot;submit&quot;], [type=&quot;button&quot;], [type=&quot;image&quot;], [role=&quot;button&quot;]&#39;)

# Print the results
print(&quot;Found&quot;, len(a_tags), &quot;&lt;a&gt; tags:&quot;)
for tag in a_tags:
    print(tag.get_attribute(&#39;href&#39;))

print(&quot;Found&quot;, len(li_tags), &quot;&lt;li&gt; tags:&quot;)
for tag in li_tags:
    print(tag.text)

print(&quot;Found&quot;, len(button_tags), &quot;&lt;button&gt; tags:&quot;)
for tag in li_tags:
    print(tag.text)

print(&quot;Found&quot;, len(clickable_items), &quot;clickable items:&quot;)
for item in clickable_items:
    print(item.get_attribute(&#39;onclick&#39;), item.get_attribute(&#39;href&#39;), item.get_attribute(&#39;type&#39;), item.get_attribute(&#39;role&#39;))

# Close the webdriver
driver.quit()

huangapple
  • 本文由 发表于 2023年8月9日 18:14:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76866754.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定