This HTML block:
<td class="tl-cell tl-popularity" data-tooltip="9,043,725 plays" data-tooltip-instant="">
<div class="pop-meter">
<div class="pop-meter-background"></div>
<div class="pop-meter-overlay" style="width: 57%"></div>
</div>
</td>
equates to this XPath:
xpath = '//*[#id="album-tracks"]/table/tbody/tr[5]/td[6]'
Trying to extract the text: 9,043,725 plays with
find_element_by_xpath(xpath).text()
returns an empty string. This text is only generated when a user hovers their mouse over the HTML block.
Is there a way to alter the XPath so that an empty string is not returned but the actual string is returned?
Try using get_attribute instead. The intended element can be located using any find_elements mechanisms. See the API DOC
element = browser.find_elements_by_css_selector('.tl-cell.tl-popularity')
text = element.get_attribute('data-tooltip')
Related
I'm using Selenium Python to locate label element.I want to use ::before to locate it,because this is a pop window.
<div class="crow" grp="0" grpname="Pizza Size">
::before
<label class="label0" cid="1">
<input type="radio" name="0" coname="M" sname="" price="9.99" value="392">M<b class="ip">9.99</b>
</label>
<label class="label0" cid="1"><input type="radio" name="0" coname="L" sname="" price="11.99" value="393">L<b class="ip">11.99</b>
</label><div style="clear:both">
</div>
</div>
I have no idea how to use ::before to locate it,any friend can help?
Pseudo Elements
A CSS pseudo-element is used to style specified parts of an element. It can be used to:
Style the first letter, or line, of an element
Insert content before, or after, the content of an element
::after
::after is a pseudo element which allows you to insert content onto a page from CSS (without it needing to be in the HTML). While the end result is not actually in the DOM, it appears on the page as if it is, and would essentially be like this:
CSS:
div::after {
content: "hi";
}
::before
::before is exactly the same only it inserts the content before any other content in the HTML instead of after. The only reasons to use one over the other are:
You want the generated content to come before the element content, positionally.
The ::after content is also "after" in source-order, so it will position on top of ::before if stacked on top of each other naturally.
Demonstration of extracting properties of pseudo-element
As per the discussion above you can't locate the ::before element within the DOM Tree but you can always be able to retrieve the contents of the pseudo-elements, i.e. ::before and ::after elements. Here's an example:
To demonstrate, we will be extracting the content of ::after element (snapshot below) within this website:
Code Block:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get('https://meyerweb.com/eric/css/tests/pseudos-inspector-test.html')
script = "return window.getComputedStyle(document.querySelector('body>p.el'),':after').getPropertyValue('content')"
print(driver.execute_script(script).strip())
Console Output:
" (fin.)"
This console output exactly matches the value of the content property of the ::after element as seen in the HTML DOM:
This usecase
To extract the value of the content property of the ::before element you can use the following solution:
script = "return window.getComputedStyle(document.querySelector('div.crow'),':before').getPropertyValue('content')"
print(driver.execute_script(script).strip())
Outro
A couple of relevant documentations:
Document.querySelector()
Window.getComputedStyle()
If I am not wrong, then, selenium doesn't provide any function or API call for this.
You can try parsing the html and then finding the tag using a simple regular expression.
Hope this helps :)
I'm trying to get text "General (8)" shown in below HTML code using selenium webdriver but kept running into issues. Any input is highly appreciated. Thanks.
my code:
test1 = driver.find_element_by_xpath("//input[#id = 'General'][#role = 'presentation']").text
print(test1)
returns null
HTML:
<li class="" role="checkbox" aria-checked="false">
<div class="extend_clickable" tabindex="0">
<input id="General" role="presentation" name="General" checked="checked" type="checkbox">
General (8)
<label for="General" role="presentation"></label>
</div>
</li>
input node is always empty. It means it cannot contain any child nodes (including text nodes). What you want is a text sibling of input which you can get as text content of parent div:
test1 = driver.find_element_by_xpath('//div[#class="extend_clickable"]').text.strip()
As per the HTML you have provided to print the text General (8) you have to extract it from the <div class="extend_clickable" tag, as the text is not within <input> tag and you can use the following code block using Python's splitlines() method as follows :
all_text = driver.find_element_by_xpath("//li[#role='checkbox']/div[#class='extend_clickable']").get_attribute("innerHTML")
myText = all_text.splitlines()
print(myText[1])
Console Output :
General (8)
Update
As per #Andersson's counter question/comment the following screenshot should address and answer all the queries.
I am trying to use Python Selenium to input a custom value in an input box on a website.
The html shows this element is stored within a table. The html and my code are shown below. I have tried sending keys on the element with class 'filterLink' and the class 'value'. Is it possible to send keys on a table data element?
Or should I be looking somewhere else in the html to send keys on this field?
The error I am getting is "Message: unknown error: cannot focus element"
Thanks very much!!
<div class="ContentSourceTypeData show" group="Provider Type">
<ul>
<li>
<table class="listItem">
<tbody>
<tr>
<td class="label">
<a class="filterLink" href="javascript:void(0);" value="bank" name="Banks" address="true">
<span class="value">Banks</span> (1831)
</a>
</td>
</tr>
.....
My code...
index_details_elem = browser.find_element_by_class_name('ContentSourceTypeData')
nameTable = index_details_elem.find_elements_by_class_name("listItem")[0] #
Select first listItem as element
nameDataElem = nameTable.find_element_by_class_name("label")
#nameInputElem = nameDataElem.find_elements_by_class_name("value") #used the above line instead of this one as the 'span' element seemed to be causing an issue
#print nameElem.location()
#nameDataElem.click() # removed as I can't click in a table
nameDataElem.send_keys("lookup value")
I would expect that you are able to send_keys to any selenium webelement. I would probably use a css selector (Maybe there is another element on the page with a name of "label"). Try doing:
webelement = "a[class=\"filterLink\"]"
webelement.send_keys("lookup value")
That will select all "a" elements, with a class value of "filterLink", and then send the keys "lookup value" to it.
HTML information is :
<a title="Create an Account" class="button" href="http://demo.magentocommerce.com/customer/account/create/">
<span>
<span>Create an Account
</span>
</span>
</a>
Create an Account
I am trying with :
create_account_button = driver.find_element_by_xpath("//button[#title='Create an Account']")
create_account_button.click()
but it is not working
Actually assign the WebElement variable, u use the following code
WebElement button = driver.findElement(By.xpath("//a[#title='Create an Account']");
button.click();
Steps
Creating a WebElement variable button and assigning the value to it.
Performing click() on that webelement
For Java follow above steps, for Python use below
driver.find_element_by_xpath('//a[#title='Create an Account']').click()
Xpath you need To use be in <a> not in <button>
So try this Xpath //a[#title='Create an Account']
Further to question here.
<a id='1234' href ="http://www.google.com' class='alpha' > MY TEXT </a>
<caption>
<em> ABCD </em>
</caption>
I want to extract text between i.e
id='1234' href ="http://www.google.com' class='alpha'
How to do this using python and selenium.
Use
web_element.get_attribute(attribute_name)
method on the web_element object to get the value of any attribute present in a web_element in this case id, href, class.