Python Selenium Find Randomly Generated Element ID - python

I have looked a few solutions on this issue on stackoverflow. Although I cannot seem to find working solution.
There are ton of elements with the id prefix "ext-gen" so most of the answers I have seen will not work.
Here is the HTML
<input type="submit" value="New Player Arrived" id="ext-gen1084">
I'm trying to find this element and then return its value with
get_attribute('value')

You can use the following to find a randomly generated element ID:
driver.find_element_by_css_selector("input[type='submit']")

Related

Distinguishing between the same web elements with Python Selenium based on relates

After third attempt of solving this problem, I am unable to finalize this on my own. I would appreciate, if someone would like to share their thoughts on the following issue.
Let us assume, we have such kind of HTML structure:
<div class="panel"></div>
<div class="title"></div>
<h3 class="title">HEADER NUMBER ONE<h3>
<div class="area"></div>
<div class="something">IO field</div>
<input class="input"></input>
<div class="panel"></div>
<div class="title"></div>
<h3 class="title">HEADER NUMBER TWO<h3>
<div class="area"></div>
<div class="something">IO field</div>
<input class="input"></input>
My intention is to identify an input element that belongs to the second panel.
Based on reliability check, when I have hardcoded XPATH gathered directly from the browser, sometimes wrong element is being identified (I assume that there are many scripts running when the page is being loaded, which impacts the reliability and stability). Therefore I would like to distinguish between elements based on the h3, which are the one and only difference between objects.
How can I do it?
When identifying elements one by one (so first the title, then its parent, and then moving down to the input), I receive an "element not interactable" exception which is not dependent from the time.
I am thinking of something like:
find //input[#class='input'] where one of ancestors contains /div/h3 which contains(text(), 'HEADER NUMBER TWO')
Obviously, I did not found any working solution for that, despite I spent more than a week with that.
Is it doable at all? If so, could you suggest me something, please? The structure in real is a little bit more complex, but I need just a pattern, hint, or clue.
Greetings!
You can locate the parent panel element based on it's child h3 with the desired title and then to locate the input element inside it.
The XPath to do so can look like the following:
"//div[#class='panel' and(.//h3[contains(.,'HEADER NUMBER ONE')])]//input"
Or even
"//div[#class='panel' and(contains(.,'HEADER NUMBER ONE'))]//input"
The selenium command using that XPath can look like:
driver.find_element(By.XPATH, "//div[#class='panel' and(contains(.,'HEADER NUMBER ONE'))]//input")
More explanations
The XPath
"//div[#class='panel' and(.//h3[contains(.,'HEADER NUMBER ONE')])]//input"
literally means:
Find element with div tag and class attribute value panel and having some child element inside it (this is .// comes for) containing HEADER NUMBER ONE text content.
Inside the above div element find input child element.
In Selenium you can find a set of elements:
inputs = driver.find_elements(By.CSS, 'input[class="input"]')
input_2 = inputs[1]
The input you need is the 2nd element in the list

Python Flask getlist of checkboxes returns empty list

I am using an html table to display some data with checkboxes as the first column, so the user can select multiple rows and delete them simultaneously. The html code to create the checkboxes is this:
<td>
<input type="checkbox" name="checkbox{{row .index}}"
value={{row .index}}>
{{row .like}}
</td>
I'm trying to get a list of all the indexes of the selected rows, so I can pass them on to a function which will delete the rows from the database. Currently I'm just using these two lines to get the list, but I get an empty list every time:
print(request.form.getlist('checkbox'))
return 'Done'
Do I have a stupid mistake somewhere?
PS. I have almost the entire html doc on one form, because it's a quite simple page and just makes things easier.
The problem is with the names of your checkbox options. They are all different as you are using a variable. Try using this name:
name="checkbox"
in this line of your code:
<input type="checkbox" name="checkbox{{row .index}}"
I finally got it to work. IoaTzimas answer was one of the things, but the ['<built-in'] problem was fixed by changing value={{row .index}} to value={{data.index(row)}}. It's quite obvious actually, because the index of row is searched for in the list 'data'. Small mistake. Thanks for the help!

Issue with locating element by text (get element attribute)

I have been struggling with this issue for longer time and finally decided to ask. I am quite new to robot framework, selenium and python and wanted to be sure that I checked everything.
So, my issue is with finding ID of a label. Below is label class which id I want to find
<label class="control-label col-sm-3" for="22_11_ahg_23" id="22_11_ahg_23-label">Full name</label>
And below is code I have written in robot framework to find this element
${full_name_id_-label}= get element attribute //*[contains(text(),'Full name')] id
Element Text Should Be id=${full_name_id_-label} Full name
2 second line is kind of debugger for me. I simply want to check whether id was found and stored in variable.
As a result I got
Element with locator 'id=' not found.
I want to store id of this label as it is dynamically changing (as well as id of input field next to it) and differences between id of input field and id of label is "-label" at the end.
I don't know how to locate input field in other way
<input id="22_11_ahg_23" class="form-control" type="text" maxlength="200" mask="" value="">
Does anyone can help me. Here are actually 2 questions:
What I am doing wrong that I cannot find element with locator -> actually why id is empty?
Is there any other way to locate such input field?
P.S. does somebody know how to get debugger working in robotframework in pycharm?
After many tries I have found an issue.
It was "*" in xpath. It should be changed with "label" to work...

How to fix a changing xPath in search box (doesn't contain text)

Basically I want to input invoices in Xero software for my job. The process is very simple, I have some values that I need to input in some slots. I have a big problem however. The xpath is dynamic (changes every time you refresh).
Basically it changes from something like this:
//*[#id="PaidToName_12ddc347c7bc4f5aa84c452f55660690_value"]
To something like this:
//*[#id="PaidToName_4fea44e4f8a844b4b630b4bf149490d8_value"]
So the numbers keep on changing.
I have tried a starts-with function however I am pretty sure that there are two XPATHs that starts with PaidToName or end with value, therefore this doesn't seem like a solution as I get this error message:
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
The other thing to note is that I see many elements that have the "input type hidden" in the HTML code which I am pretty sure play a role with that. Please let me know if there is anything I can do to help.
This is the code I have tried that doesn't work.
button = driver.find_element_by_xpath("//*[starts-with(#id,'PaidToName')]")
button.send_keys('lol')
This is the HTML code I am trying to retrieve
<input type="text" size="24" autocomplete="off" id="PaidToName_4fea44e4f8a844b4b630b4bf149490d8_value" name="PaidToName_4fea44e4f8a844b4b630b4bf149490d8_value" class="x-form-text x-form-field autocompleter x-form-focus" tabindex="10" style="width: 129px;">
You can use xpath with id and class combination, try this :
button = driver.find_element_by_xpath("//*[contains(#id,'PaidToName') and contains(#class,'x-form-text')]")
button.send_keys('lol')
Try below given locator.
driver.find_element_by_xpath("//input[contains(#id,'PaidToName') AND contains(#class,'x-form-text')]")

Selenium Find Sub-Child href Element

I'm trying to click the following link using selenium.
<div id="RECORD_2" class="search-results-item">
<a hasautosubmit="true" oncontextmenu="javascript:return IsAllowedRightClick(this);" class="smallV110" href="#;cacheurlFromRightClick=no"></a>
</div>
Which record to click is not known before the code is executed. Record_2 has multiple children, and the one included is the one I want to click. The link is edited for the sake of privacy. I tried to do something like that where name is the record variable, however it doesn't work.
driver.find_element_by_css_selector("css=div#"RECORD_%s" % (name).smallV110")
I'm a complete newbie to selenium so I couldn't figure out a way to sort this out. I would appreciate any help. Thanks!
Note that this is not Selenium IDE and you don't need the css= at the beginning of a selector.
There are multiple ways to locate the link element, e.g.:
driver.find_element_by_css_selector(".search-results-item a.smallV110")
driver.find_element_by_css_selector("[id^=RECORD] a.smallV110") # id starts with "RECORD"
If you know the id value beforehand:
id_i_know = 2
driver.find_element_by_css_selector("[id=RECORD_%d] a.smallV110" % id_i_know)
You don't have to have that smallV110 class attribute check - I've added it to increase chances of not matching other a elements inside the div (not sure what they are, you have not posted the entire HTML).

Categories