how to get attribute value using selenium and css
If your HTML consists solely of that one <a>
tag, then this should do it:
String href = selenium.getAttribute("css=a@href");
You use the DefaultSelenium#getAttribute()
method and pass in a CSS locator, an @
symbol, and the name of the attribute you want to fetch. In this case, you select the a
and get its @href
.
In response to your comment/edit:
The part after
@
tells Selenium that that part is the name of the attribute.You should place
:contains('2')
before@href
because it's part of the locator, not the attribute. So, like this:selenium.getAttribute("css=a:contains('2')@href");
How to get attribute value using Selenium - Python
To print the value of the title
attribute you have to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR and class attribute:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div[data='#results'] > a.btn.btn-white.btn-sm.btn-rounded.dropdown-toggle"))).get_attribute("title"))
Using XPATH and innerText:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@data='#results']/a[text()='results']"))).get_attribute("title"))
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Getting attribute value using Selenium
form
node has neither @name
nor @value
. Try to get attribute values from child input
node
IWebElement _frm1 = webDriver.FindElement(By.Id("frm1"));
IWebElement _input = _frm1.FindElement(By.Name("Token"));
token_frm1 = _input .GetAttribute("value");
How to extract the value of the attribute picture using Selenium Python
To extract the value of the attribue sl-video-preview
, as the element is an Angular element you have to induce WebDriverWait for the visibility_of_element_located()
and you can use either of the following Locator Strategies:
Using
CSS_SELECTOR
:print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.gallery-list > figure.figure.hd > a > picture.ng-isolate-scope.sl-safe"))).get_attribute("sl-video-preview"))
Using
XPATH
:print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='gallery-list']/figure[@class='figure hd']/a/picture[@class='ng-isolate-scope sl-safe']"))).get_attribute("sl-video-preview"))
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
How to get attribute value inside a div in webdriver
ieDriver.findElement(By.xpath("//div[@class='assign_grid assign_grid_selected']")).getAttribute("login");
I'd also check to ensure that the locators you are using are only bringing back that one element, and not more - to check this, run the same thing using .findElements
and verify only one single result is returned.
It is common that there are hidden elements in the HTML, and your locator may be picking them up.
Get href attribute of an element of a web using Selenium
To print the value of the href
attribute you can use either of the following locator strategies:
Using cssSelector:
System.out.println(wd.findElement(By.cssSelector("a.a-link-normal#vvp-product-details-modal--product-title")).getAttribute("href"));
Using xpath:
System.out.println(wd.findElement(By.xpath("//a[@class='a-link-normal' and @id='vvp-product-details-modal--product-title']")).getAttribute("href"));
Ideally, to extract the the value of the href
attribute, you have to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following locator strategies:
Using cssSelector and
getText()
:System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("a.a-link-normal#vvp-product-details-modal--product-title"))).getAttribute("href"));
Using xpath and
getAttribute("innerHTML")
:System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[@class='a-link-normal' and @id='vvp-product-details-modal--product-title']"))).getAttribute("href"));
How to locate element in Selenium with attribute only, irrespective of the what value attribute holds
You can do it with CSS Selector:
div[data-app-test]
GetAttribute in Selenium VBA for style
getAttribute("style")
should work but you have to induce a waiter for the element to be present/visible within the HTML DOM.
Debug.Print post.Item(i).getAttribute("style")
Precisely, to extract value of the style attributes from the elements you can use the getCssValue()
method as follows:
Debug.Print post.Item(i).getCssValue("z-index")
Debug.Print post.Item(i).getCssValue("top")
Debug.Print post.Item(i).getCssValue("left")
Debug.Print post.Item(i).getCssValue("width")
Debug.Print post.Item(i).getCssValue("height")
getAttribute("innerHTML")
get_attribute("innerHTML")
can be used to read the innerHTML or the text within any node / WebElement
You can find a detailed discussion in Difference between text and innerHTML using Selenium
References
You can find a couple of relevant discussions in:
- How to get child property value of a element property using selenium webdriver, NUnit and C#
- How can I verify text is bold using selenium on an angular website with C#
Related Topics
How to Use Pseudo-Elements (:After, :Before) Inside a Table Row
How to Select Every Other Group of Three in CSS
How to Disable the "Responsive" Grid in Bootstrap
Right Aligned Bootstrap Container Which Also Aligns with Default Container
Mime Type Error When Adding a CSS File to Angular
Excluding an Element from Nth-Child Pattern
How to Add CSS to Selected Row in Treegrid Gxt 3
Less Inheritance Fails with "Undefined Class"
Font Awesome 5 Pseudo-Class Not Working with My Kit
Set Line-Height as a Percentage Relative to the Parent Element
CSS Pointer-Events and Appearance Properties Not Recognized by CSS Validator
Why Bootstrap 3 Navbar Dropdown Doesn't Work in IE8
CSS - Force Overflowing Elements to Disappear If Partially Hidden
How to Remove a Specific Bullet Point Within a Ul in CSS