Find element in desplegable list after doing browser back - selenium

I'm scraping a web with the following structure
Menu1
Submenu1
Event1
Event2
...
Submenu2
Event n
...
...
Menu2
....
Where to have access to Submenu you have to click on Menu, so as to expand the node and make submenu list visible, and to have access to Event list the same, you need to click on the corresponding submenu node.
Once you get the event, you click on it and it goes to another page. After you scrape some, you go back to the previous page and scrape the next event. The code would be as follows
browser=webdriver.Chrome()
browser.get(url)
Menu1=browser.find_element_by_xpath('some expression')
Menu1.click()
submenu=Menu1.find_elements_by_xpath('some other expression')
for sub in submenu:
event=sub.find_elements_by_xpath('expression here')
for ev in event:
event.click()
Some scraping
'Go back to previous page'
browser.execute_script("window.history.go(-1)")
After doing the first iteration when trying to do the second for Event2, I get a NoSuchElementException, basically because after going back list are not expanded and therefore not all objects are visible to keep on with the for looping.
Is there any way that suits the code to fix this? My guess is that if I click Menu1 again, all Submenu elements will be visible so that list will be once again available. Afterwards, if I click on sub then the Events list will be available as well.
Eager to read your suggestions
Thanks

So, in esence, what has come to my mind as the solution is generating several list for each menu, with the text of the node. Iterate in for loops instead of nodes, loops of the node names that are unchanging.
Then, for each node name, loop through the object until finding it, and click on that particular item.
Then repeat until you've reached all nodes.

Related

Global Send Keys - Blue Prism

I am trying to use global send keys to press "Down" and then "Enter", however the element that I am trying to spy loses functionality if I use the UI Automation mode every time.
So my question is if I spy my element with the HTML mode is there still a way to use the Global Send Keys as so far it I can only see the option to use them if the UI mode is used for spying.
Any solutions would be greatly appreciated.
Thanks
Bit of incorrect information going on in this page so doing some work to steer people correctly here and then provide solution to OP's problem;
SendKeys are only utilised by the following attributes as of v6.4
UI Elements:
Button (UIA) elements
Check Box (UIA) elements
Radio Button (UIA) elements
Combo Box (UIA) elements
Edit Box (UIA) elements
Menu (UIA) elements
Menu Item (UIA) elements
List (UIA) elements
List Item (UIA) elements
Table (UIA) elements
Tab Control (UIA) elements
Tab Item (UIA) elements
Hyperlink (UIA) elements
Tree View (UIA) elements
Tree View Item (UIA) elements
Window (UIA) elements
Active Accessibility Elements:
Button (AA) elements
Combo Box (AA) elements
List Box (AA) elements
Edit (AA) elements
And lastly
Application elements (what some people call the root element)
SAP Main Windows
These are the only places you can use Send Keys.
As for Send Key events, these are for utilizing send keys typically on thin client applications like Citrix. A great explanation is posted here:
https://www.rpaforum.net/threads/global-send-keys-and-send-key-events.1587/
Also there is a data sheet on Send Keys called 'Guide to Send Keys and Send Key Events on the Blue Prism portal would recommend you read it. For the purposes of answering here to Op's various questions.
If you spy with HTML mode you can't use a HTML element to invoke Send Keys. You can use the root application sure but not the actual spied element. There is a sequence of events you could try where you use HTML/AA/similar to click the element you are targeting and then afterwards utilise either an element listed above or the root element to invoke send keys to perform the task you are looking for. In this case teh task you are trying to do in send keys is
{DOWN}
for the down arrow or
{PGDN}
for page down if you wanted instead and then
{ENTER}
for the enter function
As stated by Blue Prism and other resources unless you absolutely need to do not use Send Key Events, it is not preferred.
Yes, using a navigate stage Blue Prism allows for Global Send Keys to be utilized on elements spied in HTML mode.
This is especially useful/necessary when interacting with generatively constructed DOMs that like to mimic ractive front-end (looking at you SharePoint 2019).
Case-in-point: A dropdown that holds no values until clicked, at which point the JavaScript 'onclick' binding or shadow-DOM is activated to return the values. Thus these dropdown values are unavailable to spy/map in any consistent way, so you'd need to "navigate" and select based on Global Send Keys.

The ListBox Click event is lost when focus is set to a TabControl

I am working with a Visual Basic.NET project in Visual Studio 2017 (.NET Framework 4.6.1)
I am using a Listbox which is associated with a BindingSource.
The Listbox is contained within one pane of a SplitterControl.
I also have a TabControl is located within the other pane of the SplitterControl. This control contains child data associated with the items in the ListBox and is refreshed when the ListBox is clicked and an item is subsequently selected in the ListBox. The Click event refreshes a Treeview, which then populates the TabControl with data if/when (1) a tree node is clicked and (2) appropriate data is retrieved for that node.
The Listbox Click event works fine while clicking different items consecutively within the Listbox only.
However, once the mouse clicks in the TabControl (in the other pane of the SplitterControl), then once the initial Listbox is clicked, the Click event doesn’t fire.
I have researched this issue (on StackOverFlow as well as other forums) and suggestions have basically revolved around the idea that the Click event could be forced by calling the routine set up as the Click event handler (e.g., myListBox_Click).
I have tried this, but the problem is that I am losing the current SelectedIndex / SelectedValue.
To illustrate by reproducing the issue within the app where I am encountering the problem:
The app in question (note the Listbox highlighted by the red arrow - Screenshot 1):
Screenshot 1
Upon startup of the app, the Listbox behaves as it should, if the user clicks nowhere else, only selecting various items within the Listbox. The TabControl (initial tab shown is always the Profile tab) is refreshed accordingly.
However, if at any point, the mouse is clicked in the TabControl (e.g., to select another tab, note Screenshot 2)
Screenshot 2
Now, when the user clicks on another item in the Listbox, the wrong item is selected (shown in Screenshot 3):
Screenshot 3
The Click event is now being forced via a call to MyListBox_Click within the TabControl_Leave event; my thinking was that this was the appropriate place to put the subroutine call. My reasoning was that when the Listbox was clicked in this scenario, the first event to fire (as far as I could tell) was the TabControl_Leave event.
Not surprisingly, if the same item in the Listbox (in this example scenario, Allegheny) is subsequently clicked, than the result is what should have happened on the previous click (that the SelectedValue is correct, as seen in Screenshot 4):
Screenshot 4
Of course, this is correct, since the last click occurred while the Listbox was already the active control.
I have tried several approaches such as (1) using a global variable to store the current index/value and retrieving it, and (2) using the Tag property of the TabControl to store and retrieve the same value. This has either worked inconsistently or not at all because of the order in which the control events fire.
Hopefully, I've been clear enough on the nature of my problem/issue.
Anyone have any ideas on what my next approach could be? Any ideas/insights/suggestions will, of course, be greatly appreciated.
Thanks in advance,
Chris Fleetwood
Software Developer
North Carolina Partnership for Children (SmartStart)

Can not click on element via TestComplete object mapping

I need to click on a web element, within TestComplete, this element looks like this:
I outlined its boundary using heavy-black line.
The problem is, by default, an element is always clicked in its middle point, and for this element above, clicking its middle point produces no results as it does not click this plus sign.
Within TestComplete, this element can not be broken further down into smaller elements.
I can use Selenium in JavaScript to click it, but is there is an other way?
Thanks
Try using x and y coordinates with the Click method.
SmartBear Click Action
I needed to click a button on my website but TestComplete was only able to identify the parent object which had 3 buttons in it. I was able to use x,y coordinates to press the 3 different buttons in parent object.
Record a script where you click the button and look at the function created to get the coordinates you need.

How can I select always the last radio button?

I have a verification page to test by selenium webdriver automation. On the verification page there are always three questions.
But those questions are selected from a pool of questions and so for a single user different questions might appear every separate time he comes to verification page.
So say there are 20 questions in my pool and there are five options (radio answers) for each question, so there are 100 separate radio buttons and each has its separate id/name in DOM.
I am unable to automate this piece of the webpage.
In order to proceed with my testing, I need to always select the last radio button for each of the three questions.
The last radio always contains either "None" or "never" in the label text and radio text label is clickable.
Also the name locator always starts with "1402248" for each radio button.
I am using Page object model in my projet.
Can someone help me to understand how can I identify each radio webelement?
I am using this:
#FindBy (xpath = "//*[#class='radio']//a[contains(text(),'never') or contains(text(),'None')]")
protected WebElement oVerifyIdentityFirstAnswer;
This CSS selector should find all radio buttons that are last of their type inside a parent element
driver.findElements(By.cssSelector("input[type='radio']:last-of-type"))
If your radio buttons are all encapsuled individually, move the last-of-type to that container element:
driver.findElements(By.cssSelector("a:last-of-type" > input[type='radio']))
This returns a list of WebElement, which you can iterate over and click every entry. I wouldn't use the #FindBy annotation by the way, since it often leads to staleness exceptions.
Try changing your Xpath to:
xpath = "//*[#class='radio']//a[contains(text(),'never') or contains(text(),'None')][last()]"

Searching through DataGridView

I have a datagridview with three columns (ID, Name, Address). It's bound to a database that contains around 500 items.
I want to be able to search the gridview for data given in a text box, and then highlight it.
If possible, pressing the Next button should find the next match, and the Reset button should clear all selections (nothing highlighted).
Please advise how to do it.
Thank you very much.
Add a search box and search button in the page. OnClick of the search button, search the datatable for the results and keep the result in the session. then on the bound event of grid, highlight the first row of from the search result set (which can be done by matching the primary/unique key of the table).
You have to handle most of code the manually for this. OnClick of next button, highlight the next row from the search result. again traversing of the search result has to handled manually like keeping the track of current result, moving to next result or moving backward etc.
then on click of reset button clear the search result session and bind the grid again without any highlighted rows.
I don't have code to post for you right now. but i hope if you implement this approach surely your problems will be resolved.