Selenium 'WebElement'对象没有属性'Get_Attribute'

vsnjm48y  于 2022-12-13  发布在  其他
关注(0)|答案(4)|浏览(249)

我正在使用Selenium webdriver(chrome)和Python,我试图从网页上的所有链接中获取href。当我尝试以下操作时:

items = driver.find_elements_by_tag_name("a")
print items

for item in items:
    href = item.Get_Attribute('href')
    print href

它设法获取所有链接,但在get_attribute时,我得到一个错误:
'WebElement'对象没有属性'Get_Attribute'
虽然我到处看,它似乎应该工作。

6ojccjat

6ojccjat1#

“Get_Attribute”属性不存在,但“get_attribute”属性存在:

items = driver.find_elements_by_tag_name("a")
print items

for item in items:
    href = item.get_attribute('href')
    print href
hmae6n7t

hmae6n7t2#

对于带输入字段的python,如下所示:

nowText = driver.find_element_by_id("source").get_attribute("value")
print(nowText)
rqqzpn5f

rqqzpn5f3#

src = driver.find_element_by_css_selector("img").get_attribute("src")
kt06eoxx

kt06eoxx4#

在最新版本中,不赞成使用find_element_by_id,应使用find_element

# Import Selenium's WebDriver module
from selenium import webdriver

# Create a WebDriver object
driver = webdriver. Chrome()

# Use the get method of the WebDriver object to open the specified URL
driver.get("https://www.example.com")

# Use the find_element method to get the specified element
element = driver.find_element(By.ID, "input-id")

# Get the text in the element
text = element.text
print(text)

# Close the WebDriver object
driver. quit()

相关问题