I have an HTML element <input id="input-id" type="text" value="initial">.
After loading the page I can get text from this element using, for example, driver.find_element_by_id("input-id").get_attribute("value").
But then I click on this element and change the text inside (to "edited", for example). Nothing in DOM changes, including value of the value attribute
(i.e. driver.find_element_by_id("input-id").get_attribute("value") still returns "initial" and element in DOM looks like <input id="input-id" type="text" value="initial">)
How can I extract value that is now visible in browser (i.e. string "edited")? Do I need to execute some JavaScript or anything?
That value doesn't change when you input some new text.
You could try to do just this:
driver.find_element_by_id("input-id").get_attribute("innerHTML")
Related
So, there's a site I'm trying to parse so it can automatically raising my offers every two hours.
The site designed in that way that you have to mark with checkboxes the lots you want to raise.
Somehow in html code the checkbox doesn't have value, instead it looks like this:
I have to click it manually via using
wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "idk what to write so it checks it"))).click()
But I really don't know how do I find it so it can be clicked.
<label>
<input type="checkbox" value="613" checked="">
# value - lot id, checked - means the checkbox is marked
<label>
# and non-checked checkbox code looks like this:
<label>
<input type="checkbox" value="613">
<label>
You can't use By.CLASS_NAME here since it has no class.
You can use:
By.CSS_SELECTOR to find by CSS selectors
chbVal = '613' # in case you need be able to change this
(By.CSS_SELECTOR, f'label > input[type="checkbox"][value="{chbVal}"][checked=""]') # for checked
(By.CSS_SELECTOR, f'label > input[type="checkbox"][value="{chbVal}"]:not([checked])') # for unchecked
or By.XPATH to find by Xpath
chbVal = '613' # in case you need be able to change this
(By.XPATH, f'//label/input[#type="checkbox"][#value="{chbVal}"][#checked=""]') # for checked
(By.XPATH, f'//label/input[#type="checkbox"][#value="{chbVal}"][not(#checked="")]') # for unchecked
Note: These are just based on the html snippet you've included - there might by parent elements with better identifiers that you need to include in your path/selector.
Also,
Somehow in html code the checkbox doesn't have value
but in your snippet it does have value...? Anyway, the examples above include value, but you don't have to include them; you can even exclude them with not(...) as shown for checked. (Btw, not(checked)/not(#checked) should exclude elements that have a checked attribute at all, no matter what the value is.)
I using selenium i try sed_keys() but element on website have only name and this name is "name".
driver.find_element_by_name("name")
<input type="text" name="name" value="">
Edit:And the items on the page named = "name are two and I want to add a text to the second
Ideally you would find something unique in the DOM above the element you want and use that to build a locator that finds only the element you want. One quick fix would be to use find_elements() (note the plural) and get the second element.
driver.find_elements_by_name("name")[1].send_keys("some text")
^ the 's' will return a collection
^ use array notation to pick the one you want
The problem is if this page changes, e.g. the element order is changed, another matching element is added, etc., then this will start sending text to the wrong element again.
the method which you have given should work , jut try using xpath
your_element = driver.find_element_by_xpath('//*[#name="name"]').click()
So I have this element in html :
<input data-v-72dea36a="" type="text" name="tradelink" placeholder="Enter your code" style="margin-right: 25px;">
How can I select exactly this one? I tried:
find_element_by_name('tradelink')
There is another element before this one with the same name...it doesn't have an id or class name...
If there are just 2 elements with attribute name="tradelink", and assuming you need the seconds one, you can use:
find_elements_by_name('tradelink')[1]
Another way is using xpath to match the placeholder value:
find_element_by_xpath("//input[#placeholder='Enter your code']")
Notes:
Notice the plural on elementS on the 1st example
Selenium Docs - Locating Elements
Fore more than 2 elements, you may have to change the array item number, i.e.: find_elements_by_name('tradelink')[2]
Rightclick the element within the inspect element editor and select copy full xpath. As posted above, paste that code into the parentheses in find_element_by_xpath()
I have a web page which i am automating, I can click on any other elements using xpath or name or ID, but this gets little tricky when the element which I want to click is inside label. Here is the example,
<div class="dv-widget dv-deco-def dv-sz-med dv-map-sw">
<input name="l_maps" id="grp_perc" value="0" class="map_HD dv-radio-swr" type="radio">
<label for="grp_percentage" class="dv-radio-swl" onclick="">%</label>
<input name="l_maps" id="grp_count" value="1" class="map_HD dv-radio-swr" checked="" type="radio">
<label for="grp_multiply" class="dv-radio-swl" onclick="">*</label></div>
I need to click on the radio buton with the text % on it, I tried several option using xpath and CSS and ID but nothing seems to find that element under that label. I need help on this guys please. Thank you in advance.
This is kind of a strange situation. Typically the for attribute of the LABEL matches the ID of the INPUT that it corresponds to, e.g.
<input id="name" ... >
<label for="name" ... >
But in this case it doesn't match. We can get around it pretty easily. You can search for the element that contains the % text using XPath and then find the preceding INPUT.
//label[.='%']/preceding-sibling::input
If you plan to use this elsewhere also, I would suggest that you put this into a function and pass in the LABEL text, e.g. % and feed that into the locator to click the matching INPUT.
To click on the radio button with the text as % you can use either of the following lines of code :
Using preceding :
driver.find_element_by_xpath("//label[#class='dv-radio-swl' and #for='grp_percentage']//preceding::input[1]").click()
Using preceding-sibling :
driver.find_element_by_xpath("//label[#class='dv-radio-swl' and #for='grp_percentage']//preceding-sibling::input[1]").click()
Using ancestor :
driver.find_element_by_xpath("//label[#class='dv-radio-swl' and #for='grp_percentage']//ancestor::input[1]").click()
Update
As per the first part of the xpath I have provided it correctly identifies the <label> element with text as %. See the snapshot :
Now, appending preceding makes no mistake to identify the previous <input> tag. See snapshot :
So in all means our xpath is correct. Possibly, you need to induce a waiter for the element to be clickable as follows :
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[#class='dv-radio-swl' and #for='grp_percentage']//preceding::input[1]"))).click()
If you are still unable to locate the element check the HTML if the element is within an <iFrame> tag and you will find a detailed discussion in How can I select a html element no matter what frame it is in in selenium?
I am still fairly new to the selenium webdriver scene and need some help. I am trying to use a csv file and the value in a specific column to click a specific checkbox on a page using python.
Here are the options for the checkboxes:
<input id="GroupsElected_0" name="GroupsElected" type="checkbox" value="ALL">
<input id="GroupsElected_1" name="GroupsElected" type="checkbox" value="newtest">
<input id="GroupsElected_2" name="GroupsElected" type="checkbox" value="test">
I can make it click the checkbox if I give it the actual value:
group=browser.find_element_by_css_selector("input[type='checkbox'][value='ALL']").click()
What I want to do is make it click the box of the correct one based on the value in my .csv file. I have tried a bunch of different things with no success. I always get invalid selector or cant find the element.
Here's the latest i have:
group2=browser.find_element_by_css_selector("input[type='checkbox'][value=cells[6]]").click()
The value I want from the csv is in the 7th column, I know I can use DictReader and have it read the column names which I will do once I get this working.
HELP?!!
Your variable is inside the string so python doesn't recognize it as a variable. Try something like
group2=browser.find_element_by_css_selector("input[type='checkbox'][value=" + cells[6] + "]").click()