Hi Guys I want to put assertion on the value of toggle button(on/off) but I'm not able to fetch the value for the same.
The code I'm using to get value is:
driver.find_element_by_xpath("//*[#id=\"map1\"]/map-settings/div[3]/ul/li[2]/label/input").click()
val = driver.find_element_by_xpath("//*[#id=\"map1\"]/map-settings/div[3]/ul/li[2]/label/input").get_attribute("value")
return val
But I'm unable to do so.
The HTML code for the element is:
<li _ngcontent-xuq-65="" class="text-element">
Select All
<label _ngcontent-xuq-65="" class="toggle col-sm-8">
<input _ngcontent-xuq-65="" type="checkbox">
<span _ngcontent-xuq-65="" class="handle"></span>
</label>
</li>
Please refer to Images attached to see the snippet of toggle and the IDE recording of elements for further enlightenment.
You are trying to get a value attribute when there is no value attribute in your HTML. Use the text property instead. This will work if there actually is a value in the field you are attempting to pull.
val = driver.find_element_by_xpath("//*[#id=\"map1\"]/map-settings/div[3]/ul/li[2]/label/input").text;
Related
I want to specify the beg and end dates.
I can't change the default dates, because the input boxes are read only. Or, I have to select the dates from clicking the calendar. Don't know how to do that. Is there a way to send keys to read-only input boxes?
The sources of the two input boxes are as follows:
<div class="date-from">
<h3>From</h3>
<input type="text" readonly="readonly" value="Jan 11, 2020">
<button class="icon la-Calendar"></button>
</div>
<div class="date-to">
<h3>To</h3>
<input type="text" readonly="readonly" value="Jan 11, 2020">
<button class="icon la-Calendar"></button>
</div>
My following code gets the "Message: invalid element state" error. Thank you!
browser.find_element_by_xpath('//*[#id="bpcg9kk"]/div/div[3]/div[1]/div[2]/div[1]/div[2]/div[1]/input').clear()
browser.find_element_by_xpath('//*[#id="bpcg9kk"]/div/div[3]/div[1]/div[2]/div[1]/div[2]/div[1]/input').send_keys("Jan 01,2019")
[Update] Almost there. Using the following code. The beg-date is changed successfully. The end-date has an issue. The beg-date also ends up in the end-date box.
element = browser.find_element_by_xpath('//*[#id="bpcg9kk"]/div/div[3]/div[1]/div[2]/div[1]/div[2]/div[1]/input')
browser.execute_script("arguments[0].removeAttribute('readonly','readonly')",element)
element.clear()
element.send_keys("Jan 01, 2019")
time.sleep(5)
element2 = browser.find_element_by_xpath('//*[#id="bpcg9kk"]/div/div[3]/div[1]/div[2]/div[1]/div[2]/div[2]/input')
browser.execute_script("arguments[0].removeAttribute('readonly','readonly')",element2)
element2.clear()
element2.send_keys("Dec 31, 2019")
You can use below lines instead send_keys to write date in date input field. Basically we directly changing value of element.
element= browser.find_element_by_xpath('//*[#id="bpcg9kk"]/div/div[3]/div[1]/div[2]/div[1]/div[2]/div[1]/input')
browser.execute_script("arguments[0].setAttribute('value', ‘“Jan 01,2019"')", element);
OR
browser.execute_script(“arguments[0].value=arguments[1]", element, “Jan 01,2019”)
Another solution:
Make input field as editable by removing readonly attribute and then send keys as below
element= browser.find_element_by_xpath('//*[#id="bpcg9kk"]/div/div[3]/div[1]/div[2]/div[1]/div[2]/div[1]/input')
browser.execute_script("arguments[0].removeAttribute('readonly','readonly')",element)
element.send_keys("Jan 01,2019")
Clear field:
element2.send_keys(Keys.CONTROL + "a");
element2.send_keys(Keys.DELETE);
I am trying to input an amount in a text field which contains a default string.
HTML of that field:
<div class="InputGroup">
<span class="InputGroup-context">$</span>
<input autocomplete="off" class="Input InputGroup-input" id="amount" name="amount" type="text" maxlength="12" value="0.00">
</div>
When trying to input text to the field, instead of replacing the default text, it appends it to it.
I have tried to use amount.clear() (amount is what I am calling the element) but after running that and sending the keys it throws the below exception:
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
As you will see from my current code, I am also trying to double click the default text but that doesn't help.
This is my code at the moment:
ActionChains(driver).move_to_element(amount).click(amount).click(amount).perform()
amount.send_keys(Keys.BACKSPACE)
amount.send_keys('100')
Which results in an input field of 0.00100 when I'm expecting 100.
Some JS based webs apps may not clear the input fields properly.
You can try using Actions class to click and clear a field fully. For example:
elem = driver.findElement(By.id("amount"))
actions.move_to_element(elem).click().build().perform()
actions.send_keys(Keys.BACKSPACE)
.send_keys(Keys.BACKSPACE)
.send_keys(Keys.BACKSPACE)
.send_keys(Keys.BACKSPACE).build().perform()
I have used the below code to click the element.But it failed to locate the element and shows element not visible.
elem3=driver.find_element_by_xpath(".//*[#id='check-box']")
elem3.click()
The html code:
<span id="Some-span" class="urCWhl" title="Indicator">
<input id="check-box" class="urC" type="checkbox" hidefocus="hidefocus" ti="-1" tabindex="-1" ct="C"/>
<span id="label-lbl" class="name_class" style="width:100%;box-sizing:border-box;" unselectable="on" f="some-id" ti="0" tabindex="0" title="Indicator"></span>
You can try something like this:
element = driver.find_element_by_xpath(".//*[#id='check-box']")
driver.execute_script("arguments[0].click();", element)
The input might be inside a frame? If so switch to that frame by doing:
driver.switch_to_frame('framename')
or if not, try to find via id then click the element:
driver.find_element_by_id('check-box').click()
one thing to remember is that if the checkbox is already have a value, if you click the checkbox, the check will be removed. if you want to have the checkbox to have a true value always, you may do this:
driver.execute_script("document.getElementById('check-box').setAttribute('checked','');")
This will execute a javascript to always have a true value on the checkbox
<li id="add-to-cart" class="">
<input type="button" value="Add to Cart" class="primary" name="add-to-cart">
</li>
I want to print value
Output: Add to Cart
Here my solution:
first get elements inside < li> (Maybe there will be more than one):
elements = browser.find_elements_by_xpath("//li[#id='add-to-cart']//input")
for e in elements:
print(e.get_attribute("name"))
From Selenium docs:
find the button element using one of the find_element_by... methods on a driver (or on a parent WebElement)
read the name of the button or value of an input using .get_attribute(attribute_name)
read the text using property .text
How do I locate an input field via its label using webdriver?
I like to test a certain web form which unfortunately uses dynamically generated
ids, so they're unsuitable as identifiers.
Yet, the labels associated with each web element strike me as suitable.
Unfortunately I was not able to do it with the few suggestions
offered on the web. There is one thread here at SO, but which did not
yield an accepted answer:
Selenium WebDriver Java - Clicking on element by label not working on certain labels
To solve this problem in Java, it is commonly suggested to locate the label as an anchor via its text content and then specifying the xpath to the input element:
//label[contains(text(), 'TEXT_TO_FIND')]
I am not sure how to do this in python though.
My web element:
<div class="InputText">
<label for="INPUT">
<span>
LABEL TEXT
</span>
<span id="idd" class="Required" title="required">
*
</span>
</label>
<span class="Text">
<input id="INPUT" class="Text ColouredFocus" type="text" onchange="var wcall=wicketAjaxPost(';jsessionid= ... ;" maxlength="30" name="z1013400259" value=""></input>
</span>
<div class="RequiredLabel"> … </div>
<span> … </span>
</div>
Unfortunately I was not able to use CSS or XPATH expressions
on the site. IDs and names always changed.
The only solution to my problem I found was a dirty one - parsing
the source code of the page and extract the ids by string operations.
Certainly this is not the way webdriver was intended to be used, but
it works robustly.
Code:
lines = []
for line in driver.page_source.splitlines():
lines.append(line)
if 'LABEL TEXT 1' in line:
id_l1 = lines[-2].split('"', 2)[1]
You should start with a div and check that there is a label with an appropriate span inside, then get the input element from the span tag with class Text:
//div[#class='InputText' and contains(label/span, 'TEXT_TO_FIND')]/span[#class='Text']/input