GVKun编程网logo

如何从selenium中获取元素的属性?(selenium 获取元素属性)

15

本篇文章给大家谈谈如何从selenium中获取元素的属性?,以及selenium获取元素属性的知识点,同时本文还将给你拓展c#–如何从selenium中的href链接获取属性值、PythonSelen

本篇文章给大家谈谈如何从selenium中获取元素的属性?,以及selenium 获取元素属性的知识点,同时本文还将给你拓展c# – 如何从selenium中的href链接获取属性值、Python Selenium 获取元素找不到元素、Python以及如何从Selenium元素WebElement对象获取文本?、Selenium Webdriver:如何找到元素的所有属性?等相关知识,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

如何从selenium中获取元素的属性?(selenium 获取元素属性)

如何从selenium中获取元素的属性?(selenium 获取元素属性)

我正在Python中使用Selenium。我想获取.val()一个<select>元素的,并检查它是否是我所期望的。

这是我的代码:

def test_chart_renders_from_url(self):    url = ''http://localhost:8000/analyse/''    self.browser.get(url)    org = driver.find_element_by_id(''org'')    # Find the value of org?

我怎样才能做到这一点?Selenium文档似乎有很多关于选择元素的内容,但是与属性无关。

答案1

小编典典

您可能正在寻找get_attribute()。一个例子示此处以及

def test_chart_renders_from_url(self):    url = ''http://localhost:8000/analyse/''    self.browser.get(url)    org = driver.find_element_by_id(''org'')    # Find the value of org?    val = org.get_attribute("attribute name")

c# – 如何从selenium中的href链接获取属性值

c# – 如何从selenium中的href链接获取属性值

我想从“a href”属性获取链接
<a href="http://fgkzc.downloader.info/download.PHP?id=bc56585624bbaf29ebdd65d0248cb620" rel="nofollow">Download</a>

我在做什么:

ReadOnlyCollection<IWebElement> lists1 = driver.FindElements(By.ClassName("dl_link"));

string s = lists1[0].GetAttribute("a href");

我正在使用类“dl_link 1”获取元素,但我无法获取它的链接,字符串为空?

解决方法

您需要使用实际属性名称调用GetAttribute().更换:
lists1[0].GetAttribute("a href");

有:

lists1[0].GetAttribute("href");

Python Selenium 获取元素找不到元素

Python Selenium 获取元素找不到元素

如何解决Python Selenium 获取元素找不到元素?

我有以下元素。 我尝试使用 css / xpath / class 来定位元素,但失败了。 我认为失败的原因是图片底部的另一个 img 共享相同的类和属性名称。 我也使用了 find_elements 但它也无法检测到该元素。

我什至使用了绝对路径,但这不能 get_attribute(''src'') ''/html/body/div[1]/div[2]/div/main/div[1]/div[1]/div[2]/div[1]/div[1]/div[2]/div/ul/li[2]/button/picture/img''

作为参考,第二个 img 的完整 xpath:

/html/body/div[1]/div[2]/div/main/div[1]/div[1]/div[2]/div[1]/div[1]/div[2]/div/ul/li[3]/button/picture/img

enter image description here

我正在尝试从以下产品页面保存产品 img。 https://www.sephora.com/product/squalane-vitamin-c-rose-oil-P416563?icid2=skugrid:p416563

解决方法

您可以对 same tagsame attributes 进行 xpath 索引:-

//img[contains(@class,''css'')]

假设上面的 xpath 表示 DOM 中的多个 web 元素。

你可以这样定位第一个元素:

(//img[contains(@class,''css'')])[1]

像这样的第二个网络元素:

(//img[contains(@class,''css'')])[2]

等等..

或者你也可以使用 find_elements :

all_names = driver.find_elements(By.XPATH,"//img[contains(@class,''css'')]")
all_names[0].click() #to click on first element

all_names[1].click() #to click on seconf element
,

您可以根据它的 src 属性定位此元素,如下所示:

//img[contains(@src,''sku/s2382166-main-zoom'')]

driver.find_element_by_xpath("//img[contains(@src,''sku/s2382166-main-zoom'')]")

UPD
这应该适用于该网站上的所有类似页面

driver.find_element_by_xpath("//div[@data-comp=''Carousel '']//img[contains(@src,''-main-zoom'')]")
,

您可以根据具有相同名称和属性的多个标签的索引尝试以下XPath,只需更改它的索引[1][2]

((.//button[@css-1flj7f7'']//img)[1])

Python以及如何从Selenium元素WebElement对象获取文本?

Python以及如何从Selenium元素WebElement对象获取文本?

如何解决Python以及如何从Selenium元素WebElement对象获取文本??

一旦找到元素,就可以使用文本方法。

例:

for element in self.driver.find_elements_by_tag_name(''img''):
       print element.text
       print element.tag_name
       print element.parent
       print element.location
       print element.size

解决方法

我正在尝试通过使用Selenium方法来获取html页面中的标记文本内容,但似乎该方法someElement.getText()在Python中不可用。有什么帮助吗?

追溯:

AttributeError: ''WebElement'' object has no attribute ''getText''

Selenium Webdriver:如何找到元素的所有属性?

Selenium Webdriver:如何找到元素的所有属性?

在Python Selenium模块中,一旦有了WebElement对象,我可以使用以下命令获取其任何属性的值get_attribute()

foo = elem.get_attribute(''href'')

如果命名的属性''href''不存在,None则返回。

我的问题是,如何获取元素具有的所有属性的列表?似乎没有get_attributes()get_attribute_names()方法。

我正在为Python使用Selenium模块的2.44.0版本。

答案1

小编典典

这是 不可能的 使用硒webdriver的API,但你可以_执行JavaScript代码_来获取所有属性:

driver.execute_script(''var items = {}; for (index = 0; index < arguments[0].attributes.length; ++index) { items[arguments[0].attributes[index].name] = arguments[0].attributes[index].value }; return items;'', element)

演示:

>>> from selenium import webdriver>>> from pprint import pprint>>> driver = webdriver.Firefox()>>> driver.get(''https://stackoverflow.com'')>>> >>> element = driver.find_element_by_xpath(''//div[@]/a'')>>> attrs = driver.execute_script(''var items = {}; for (index = 0; index < arguments[0].attributes.length; ++index) { items[arguments[0].attributes[index].name] = arguments[0].attributes[index].value }; return items;'', element)>>> pprint(attrs){u''class'': u''topbar-icon icon-site-switcher yes-hover js-site-switcher-button js-gps-track'', u''data-gps-track'': u''site_switcher.show'', u''href'': u''//stackexchange.com'', u''title'': u''A list of all 132 Stack Exchange sites''}

为了完整起见,一种替代解决方案是获取标记outerHTML并使用HTML解析器解析属性。示例(使用BeautifulSoup):

>>> from bs4 import BeautifulSoup>>> html = element.get_attribute(''outerHTML'')>>> attrs = BeautifulSoup(html, ''html.parser'').a.attrs>>> pprint(attrs){u''class'': [u''topbar-icon'',            u''icon-site-switcher'',            u''yes-hover'',            u''js-site-switcher-button'',            u''js-gps-track''], u''data-gps-track'': u''site_switcher.show'', u''href'': u''//stackexchange.com'', u''title'': u''A list of all 132 Stack Exchange sites''}

今天关于如何从selenium中获取元素的属性?selenium 获取元素属性的介绍到此结束,谢谢您的阅读,有关c# – 如何从selenium中的href链接获取属性值、Python Selenium 获取元素找不到元素、Python以及如何从Selenium元素WebElement对象获取文本?、Selenium Webdriver:如何找到元素的所有属性?等更多相关知识的信息可以在本站进行查询。

本文标签: