Message - "unknown error: cannot focus element" - in python selenium driver - python

<div id="ccodeinput">
<input class="dropdownheader2" type="TEXT" name="CCODE" value="435435" size="14" maxlength="22">
</div>
This is the code for the Search field(see screenshot - upper right side). I am unable to pass values from a list to this search bar. But I am able to press the search button beside it.
How can I enter values in the search field?
This is what I have tried:
inputElement = chrome_driver.find_element_by_id('ccodeinput')
inputElement.send_keys(435435)

As per the HTML you have shared the <input> tag is a child of <div> tag, so to put text within the intended element you can use :
chrome_driver.find_element_by_xpath("//input[#class='dropdownheader2' and #name='CCODE']").send_keys("435435")

Related

Python Selenium Dynamic table cell returning empty string

I am trying to get value from a dynamic loading table, when I click on the TD element it works all ok.
But when I am tryin to get the text from same TD , it is returning empity string.
I have tried XPATH, CSS_Selector
Below is the UI look, I want to get the amount
Below is the HTML Snap
amount = driver.find_element_by_xpath('//*[#id="Table_Cheque_Ref_Details"]/tbody/tr[2]/td[11]').click
Working Fine
amount=driver.find_element_by_xpath('//*[#id="Table_Cheque_Ref_Details"]/tbody/tr[2]/td[11]').text
print amount
returns empity string
amount = WebDriverWait(driver,
10).until(EC.text_to_be_present_in_element((By.XPATH,
'//*[#id="xfe38"]'), '0')) print amount
Timeout Exception
Solution
As #Andersson already mentioned, you can use get_attribute('value') to extract the text from input field. Let's create the following example:
driver.get('http://demo.automationtesting.in/Register.html') # get sample page
time.sleep(3) # pause to wait until page loads
input_el = driver.find_element_by_xpath("//*[#id='basicBootstrapForm']/div[4]/div/input") # phone input field
input_el.click()
input_el.send_keys("123")
print(input_el.text) # prints nothing
print(input_el.get_attribute("value")) # prints '123'
Analysis
If we go to the dev tools:
we will see the following HTML for our input:
<input type="tel" class="form-control ng-touched ng-dirty ng-valid-parse ng-invalid ng-valid-required ng-invalid-pattern" ng-model="Phone" required="" pattern="^\d{10}$">
it does not have any text in the tag, that's why calling
print(input_el.text)
will print nothing. If the HTML would look like:
<label class="col-md-3 col-xs-3 col-sm-3 control-label">Email address*</label>
then
print(label_el.text)
would print the text inside label tag - Email address*.

How to find an element with respect to another element through selenium and python

<div class="block-inner">
<div class="form-group">
<label>Username</label>
<input type="text" name="username" class="required form-control" value="" placeholder="Enter your username...">
</div>
<div class="form-group w_margin">
<label>Password</label>
<input name="password" type="password" class="required form-control" style="" placeholder="Enter your password...">
</div>
</div>
Context
My need is to find Username in Web page and automatically has to move to the next element to fill value, and once it finds Password text and has to move to next element to fill password from dictionary. Please help me on this.
driver = webdriver.Chrome()
driver.get("https://account.genndi.com/login")
element = "//label[contains(text(),'')]" #xpath
dictCredentials = {'Username':'abcdef', 'Password':'123'}
for clas in driver.find_elements_by_xpath(element):
text = clas.text
if text in dictCredentials.keys():
inputbox = #please help here get corresponding input element
inputbox.send_keys(dictCredentials[text])
I know how to send keys directly into text box and it is easy too. But I wish to understand how to do the same operation indirectly with another class element.
To achieve this let me explain how you are going to do this.
So first you need to get the element of Username.
You have already done this with:
element = driver.findElement(by.xpath("//label[contains(text(),'')]"));
What you can also do is, get the parent element directly by saying:
element = driver.findElement(by.xpath("//label[contains(text(),'Username')]/.."));
With the /.. you are getting the ancestor of the element label with text Username
Now that you have found the element within which your input is located you can say:
inputElement = element.findElement(by.xpath("./input"));
This way, within the parent element you found, you are going to look for an input field.
As per the HTML you have shared to find the <label> tags with text as Username and Password and move to the next element to fill value you can create a function which will accept the <label> tag text and the text-to-send as two arguments and fill up the next <input> as follows :
def fillUp(labelText, inputValue):
driver.find_element_by_xpath("//label[.='" + labelText + "']//following::input[1]").send_keys(inputValue)
Now, you can call this function from anywhere within your script as follows :
fillUp("Username", "Chandrasekar ")
fillUp("Password", "Subramanian")
Just use "by_name" locator =)
user = {"name": "Alex", "password": 'pass'}
driver.find_element_by_name("username").send_keys(user.name)
driver.find_element_by_name("password").send_keys(user.password)
If you really want to find "Username" text in parent element - you have few other options:
First is to find wrapper element, that contains specific text, and search for input in it:
self.driver.find_element_by_xpath("//*[label[text()='Username']]/input").send_keys("name")
self.driver.find_element_by_xpath("//*[label[text()='Password']]/input").send_keys("pass")
Or search for element's sibling input:
self.driver.find_element_by_xpath("//label[text()='Username']/following-sibling::input[1]").send_keys("pass")
self.driver.find_element_by_xpath("//label[text()='Password']/following-sibling::input[1]").send_keys("pass")
You can get it with this pseudo xpath:
//*[#name='username']
Then:
//*[#name='password']
The "move", you can achieve with your code following this pattern:
click on the input field
clear filed
enter text
move to the next element
clear filed
enter text

InvalidElementStateException: Message: invalid element state: Element is not currently interactable and may not be manipulated

I'm using SeleniumLibrary for Robot Framework and i do have an issue regarding of Input Text function:
HTML:
<div class="form-group">
<label class="col-sm-6 control-label" for="gngFeeValue" >What is the expected Fee Value?</label>
<div class="col-sm-4">
<input id="gngFeeValue" data-role="numerictextbox"
data-format="###,###,###,###,###,##0"
data-decimals="0"
data-spinners="false"
data-bind="value: gng.feeValue, disabled: isInputDisabled" placeholder="Enter fee value" />
</div>
</div>
TEST.robot:
Expected Fee Value - Value
[Arguments] ${expectedFeeValue}
Input Text //*[#id="gngFeeValue"] ${expectedFeeValue}
I do get an error:
InvalidElementStateException: Message: invalid element state: Element is not currently interactable and may not be manipulated
Anyone who could help me regarding this? Thanks!
I had the same problem. I couldn't find any elements on a specific form.
I realized that I could use the elements outside the form normally. So I started investigating html and found that it was inside an iframe.
To solve this, I only needed the SeleniumLibrary Select Frame:
Select Frame xpath=//*[#id="iframe"]
The webElement your are trying to interact with is either not clickable or it is disabled or may be it is out of browser's viewport. Selenium API is successful only on active interact-able webelements. Use Javascript, as a last mile solution.

Web Scraping with Python: Iinput text and click a button

I was doing some web scraping with python (Linkedin site) and got stuck with the following 2 issues: 1) How do I input text on a search bar? 2) How to click a button? First, this is the search bar code:
<input aria-autocomplete="list" autocomplete="off" spellcheck="false"
placeholder="Búsqueda" autocorrect="off" autocapitalize="off" id="a11y-
ember6214" role="combobox" class="ember-text-field ember-view" aria-
expanded="false">
To input the text I was using the xpath (and it works) but it changes every time I login into the site:
search = driver.find_element_by_xpath('//*[#id="a11y-ember997"]')
search.send_keys('MedMake')
So could I use instead part of the input bar code above so that I can rerun my script multiple times?
My second point is 2) how to click a button. Again I was using the xpath but it changes after every login. My code was:
button = driver.find_element_by_xpath('//*[#id="nav-search-controls-wormhole"]/button')
button.click()
I inspected the button code and I would instead like to use data-vertical="PEOPLE" or any other of this unique fields (the tag button is not enough since there are many buttons on Linkedin site). By the way,how are all these inner fields called? I believe part of my problem arises from the lack of html code understanding.
<button data-vertical="PEOPLE" data-control-
name="vertical_nav_people_toggle" data-ember-action="" data-ember-
action-8620="8620" data-is-animating-click="true">
Gente
</button>
If id attribute values are dynamic you can use other attributes with static values:
search = driver.find_element_by_xpath('//input[#placeholder="Búsqueda"]')
search.send_keys('MedMake')
button = driver.find_element_by_xpath('//button[normalize-space()="Gente"]')
button.click()
First one use xpath
//input[contains(#class,'ember-text-field')]
Second one use the xpath
//button[#class='vertical_nav_people_toggle']

How do I select an element within a table using Python Selenium?

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.

Categories

Resources