How to convert selenium webelelements to list of strings in python - string

I have gathered obligatory data from the scopus website. my outputs have been saved in a list named "document". when I use type method for each element of this list, the python returns me this class:
"<class'selenium.webdriver.firefox.webelement.FirefoxWebElement'>"
In continius in order to solve this issue, I have used text method such this:
document=driver.find_elements_by_tag_name('td')
for i in document:
print i.text
So, I could see the result in text format. But, when I call each element of the list independently, white space is printed in this code:
x=[]
for i in document:
x.append(i.text)
print (x[2]) will return white space.
What should I do?

As you have used the following line of code :
document=driver.find_elements_by_tag_name('td')
and see the output on Console as :
"<class'selenium.webdriver.firefox.webelement.FirefoxWebElement'>"
This is the expected behavior as Selenium prints the reference of the Nodes matching your search criteria.
As per your Code Attempt to print the text leaving out the white spaces you can use the following code block :
x=[]
document = driver.find_elements_by_tag_name('td')
for i in document :
if (i.get_attribute("innerHTML") != "null") :
x.append(i.get_attribute("innerHTML"))
print(x[2])

My code was correct. But, the selected elements for displaying were space. By select another element, the result was shown.

Related

Replace a column of numbers with the associated label from another text file using the index on the text file

I'm trying to loop through column of numbers and replace a with the associated label from another text file using the index on the text file. But it keeps returning an error
#conditions.txt has the labels while 'Reason for absence' has the numbers
with open('conditions.txt') as f:
lines = f.readlines()
ds = pd.DataFrame(lines)
index=ds.index
print(index)
newname=df['Reason for absence'].copy()
for value in newname:
for i in index:
if value in newname == index :
newname=ds
This issue seems to be related to another posted question. While the approaches are different, possibly you are looking for the same end result. Take a look at the provided solution here. If the solution is not helpful, request you provide the error message.

How to get text which is inside the span tag using selenium webdriver?

I want to get the text which is inside the span. However, I am not able to achieve it. The text is inside ul<li<span<a<span. I am using selenium with python.
Below is the code which I tried:
departmentCategoryContent = driver.find_elements_by_class_name('a-list-item')
departmentCategory = departmentCategoryContent.find_elements_by_tag_name('span')
after this, I am just iterating departmentCategory and printing the text using .text i.e
[ print(x.text) for x in departmentCategory ]
However, this is generating an error: AttributeError: 'list' object has no attribute 'find_elements_by_tag_name'.
Can anyone tell me what I am doing wrong and how I can get the text?
Problem:
As far as I understand, departmentCategoryContent is a list, not a single WebElement, then it doesn't have the find_elements_by_tag_name() method.
Solution:
you can choose 1 of 2 ways below:
You need for-each of list departmentCategoryContent first, then find_elements_by_tag_name().
Save time with one single statement, using find_elements_by_css_selector():
departmentCategory = driver.find_elements_by_css_selector('.a-spacing-micro.apb-browse-refinements-indent-2 .a-list-item span')
[ print(x.text) for x in departmentCategory ]
Test on devtool:
Explanation:
Your locator .a-list-item span will return all the span tag belong to the div that has class .a-list-time. There are 88 items containing the unwanted tags.
So, you need to add more specific locator to separate the other div. In this case, I use some more classes. .a-spacing-micro.apb-browse-refinements-indent-2
You're looping over the wrong thing. You want to loop through the 'a-list-item' list and find a single span element that is a child of that webElement. Try this:
departmentCategoryContent = driver.find_elements_by_class_name('a-list-item')
print(x.find_element_by_tag_name('span').text) for x in departmentCategoryContent
note that the second dom search is a find_element (not find_elements) which will return a single webElement, not a list.

Can't acess dynamic element on webpage

I can't acess a textbox on a webpage box , it's a dynamic element. I've tried to filter it by many attributes on the xpath but it seems that the number that changes on the id and name is the only unique part of the element's xpath. All the filters I try show at least 3 element. I've been trying for 2 days, really need some help here.
from selenium import webdriver
def click_btn(submit_xpath): #clicks on button
submit_box = driver.find_element_by_xpath(submit_xpath)
submit_box.click()
driver.implicitly_wait(7)
return
#sends text to text box
def send_text_to_box(box_xpath, text):
box = driver.find_element_by_xpath(box_xpath)
box.send_keys(text)
driver.implicitly_wait(3)
return
descr = 'Can't send this text'
send_text_to_box('//*[#id="textfield-1285-inputEl"]', descr)' #the number
#here is the changeable part on the xpath
:
edit: it worked now with the following xpath //input[contains(#id, 'textfield') and contains(#aria-readonly, 'false') and contains (#class, 'x-form-invalid-field-default')] . Hopefully I found something specific on this element:
You can use partial string to find the element instead of an exact match. That is, in place of
send_text_to_box('//*[#id="textfield-1285-inputEl"]', descr)' please try send_text_to_box('//*[contains(#id,"inputEl")]', descr)'
In case if there are multiple elements that have string 'inputE1' in id, you should look for something else that remains constant(some other property may be). Else, try finding some other element and then traverse to the required input.

How to verify ANY text is present with selenium IDE

I know how to verify if a specific text is present in a web page using Selenium IDE. But what I wanted to know is, can you verify that any text is present in an element?
For example there's a text box with the title "Top Champion". This text box will be changed daily with the name of a person. Now I just wanted to check whether there is a text in this text box, no matter what the text actually is. I've tried the verify text command and tried blanking the value, but it doesn't work. If the command can return a true or false command that would be really helpful
BTW, verify value doesn't work either since the element that I'm testing is not a form field
Your best bet is as follows (I have written single tests for this for numbers)
Medium rigour:
waitForText | css=.SELECTORS | regex:.+?
This will wait until there is at least 1 character present.
Strong rigour (only works if you have a subset of characters present):
waitForText | css=.SELECTORS | regex:^[0-9]+$
This will wait until there is text. This text must start with a number, have at least 1 number, and then finish. It does not permit any character outside of the subset given. An example you could do to match numbersNAMEnumbers would be.
waitForText | css=.SELECTORS | regex:^[0-9]+[a-zA-Z]+[0-9]+$
This would wait for a string such as 253432234BobbySmith332
Luke
If i have understood your question properly there below is one way you can search for an element contains a string. Not sure if this is what you are looking.
List<WebElement> findElement = webElement.findElements(By.xpath("YOUR_TEXTINPUT_PATH_HERE"));
if( findElement.size() > 0 ){
if( findElement.get(0).getText() != null && findElement.get(0).getText().indexOf("THE_STRING_THAT_YOU_WANT_TO SEARCH") != -1 ) {
// IF IT COMES HERE, THAT MEANS THE ELEMENT IS PRESENT WITH THE TEXT
}
}
store text|[your element]|StoredText
execute script|return ${StoredText}.length > 0|x
assert|x|true
Using these three lines in the Selenium IDE, the first line will extract the text from the element into the variable StoredText.
The second line will store whether the length of that text is greater than zero into the variable x (a true or false result).
The third line asserts that the result was true, failing the test if not. You don't need the third line if all you want is the true or false result.
So if the element contains any text, the extracted text length will be greater than zero, the variable x will be true, and the assert will pass. This verifies that any text is present in the element.

In Watir, how to get the full text, from a portion of text?

I have a portion of HTML that looks similar to:
<table><tbody><tr>
<td><div> Text Goes Here </div></td>
<td> ... rest of table
There are no IDs, no Titles, no descriptors of any kind to easily identify the div that contains the text.
When an error occurs on the page, the error is inserted into the location where "Text Goes Here" is at (no text is present unless an error occurs). Each error contains the word "valid".
Examples: "The form must contain a valid name" or "Invalid date range selected"
I currently have the Watir code looking like this:
if browser.frame(:index => 0).text.includes? "valid"
msg = # need to get full text of message
return msg
else
return true
end
Is there any way to get the full text in a situation like this?
Basically: return the full text of the element that contains the text "valid" ?
Using: Watir 2.0.4 , Webdriver 0.4.1
Given the structure you provided, since divs are so often used I would be inclined to look for the table cell using a regular expression as Dave shows in his answer. Unless you have a lot of nested tables, it is more likely to return just the text you want.
Also if 'valid' may appear elsewhere then you might want to provide a slightly larger sample of the text to look for
. browser(:cell => /valid/).text
Try this
return browser.div(:text => /valid/).text
or
return browser.table.div(:text => /valid/).text
if the valid is not found, it should return nil.

Resources