Finding element in a dropdown list - Rselenium - rselenium

I'm new to RSelenium working on a web-scraping project and need help in selecting a value from a drop down list. I've tried different ways including Xpath as the code below but failed all the times.
stateselect <- remDr$findElement(using = 'xpath', value = '//*[#id="state"]')
stateselect$clickElement()
Here is the url: https://www.cbd.gov.au/get-assessed/how/find-rated-building
I want to click to select a State (e.g., ACT) and click on Search button
Any advice or help would be useful and appreciated.
inspect page

Related

SeleniumWebdriver_How To select particular link when from search results

Am new in selenium webdriver & right now I'm struggling at one point for that I need some guidelines. I'm searching employee records in search results &for require employee record I have set excel sheet for input purpose so I'm able to do that successfully but now the issue is that when a search returns me the require results I'm not able to click on that record can someone suggest me what I'm doing wrong .
I have designed below code for that driver.findElement(By.cssSelector("#txtSearchKeyword")).sendKeys(Sheet.getCell(3, i).getContents());
driver.findElement(By.name("")).click();
driver.findElement(By.xpath("(//input[#type='button'])[8]")).click();
so per above script I'm able to search employee records by providing input from excel sheet but not able to click that records to move forward. any suggestion on these will really appreciated.
#Amit once you click on search give some wait condition and then click on the link which you are expecting. because the code gets executed line by line while the page loads it is searching for the link to click , so give some wait until you see the link and then click on the link.

outputting checkbox ids to an empty div

I am currently building a keyword bank to assist in tagging my in-house design assets. My idea is to have each keyword as a checkbox form element with one empty div at the bottom. You'd go through the entire list, check what you need and the text assigned to the id of that checkbox would populate in the empty div, separated by commas, and I can just copy/paste that text right into the metadata. I plan on having hundreds, so I'm hoping there is simple solution to this.
I have no problem setting up the html form/checkboxes, but I am unsure how to make this text populate in that empty div—or even IF it's possible. Would I use jQuery? Ajax? I also don't need a submit button—just that div to update immediately each time a checkbox is touched. Would I also need to have some server-side communication with a submit button to even make this work?
Thank you so much in advance for any help you can give or any direction you can point me in.
Here is a working fiddle
What you need is basically something like this :
$( ":checkbox" ).click(function() {
//alert($( this ).val());
$("#myText").append($(this).val() + ";");
});
You can forget the alert. This will add the value assigned to the checkbox in the div followed with a coma. :)

How to count elements on web page?

Open one web page say gmail.
Want to count that how many text field or buttons or checkbox or hyperlinks or other html elements are present.
The solution is very simple . This can be done using simple XPath.
Find the type of element count you need.
eg for text fields: xpath can be : input[type='text']
for radio buttons: xpath can be : //input[#type='radio']
for check box buttons: xpath can be : //input[#type='checkbox']
so find all the elements in the page by using simple command using the above xpath:
webdriver.findElements(By.xpath("REQUIRED_XPATH")).size();
will give you the number of elements in the particular web page.
I am using c#. If you use c# as well the followings is the something you want to use. And you decide what tags you need inside the selector. I am using all the tags so far.
ReadOnlyCollection<IWebElement> webElements = Driver.FindElements(By.CssSelector("input, select, textarea, a, button"))//and keep adding
//then do a simple count. The trick here is the selector and you need to make sure you are adding all the tag names are being used in your application
webElements.Count();
If you want to count all elements of a page, you can simply use * for that as shown below.
List<WebElement> items = driver.findElements(By.cssSelector("*"));
System.out.println(items);
Here is how to know instantly how many items in the list on the website, for example, languages in the wiki, using right-click "Inspect element".
Screenshot

StaleElementReference Exception

I am struggling with the StaleElementReferenceException problem. I have seen quite a number of discussions on the topic but I haven't been able to find a solution to the issue that I am facing.
The test is to get all the links on the footer of the web page and then verify whether the link is active or not by clicking on it and verifying the title of the page.
First of all, I find & store all the links in an array list. I compare the link name with the values retrieved from the database. Then for each link, I click on it and verify the page title. Then using 'driver.navigate.back()', go back to the original page and continue with the rest of the links.
However, when the control returns back to the page, the StaleElementReferenceException occurs.
Can anyone suggest me a way out of this?
Thanks,
Anuj
When you are storing all the links in the footer you are grabbing those elements as they are at that point in time. Upon navigating to a different page those particular elements no longer exist. When you return to the back these elements have been created anew.
While the elements are the same via identifiers, they are different instances and thus your old elements in your array are 'stale'.
I would suggest storing only the link identifiers (not the link elements themselves) as strings and then search for them each time the page has loaded.
I faced a similar issue, in my case when I type something into a text box it would navigate to another page, so while I come back on the previous page, that object gets stale.
So this was causing the exception, I handled it by again initialising the elements as below -
PageFactory.initElements(driver, Test.class);
So when you navigate back, make sure you are initialising all the elements of that page again, so that the object does not get stale.
You can handle going and coming to new tab as follows:
String baseHandle = driver.getWindowHandle();
Set<String> sr = driver.getWindowHandles();
if (sr.size()>1){
Set<String> sr1 = driver.getWindowHandles();
sr1.remove(baseHandle);
Iterator itr = sr1.iterator();
driver.switchTo().window(itr.next().toString());
System.out.println("Page Title is : " + driver.getTitle());
driver.close();
driver.switchTo().window(baseHandle);

ZK window not unique in ID space

In our project we use ZK for webpages. There is a combobox which has lists. When selected, it fetches data from a java object through onSelect, i have given the logic.
when i select one there are 4 listboxes on that page to be filled with data according to the selection. when i select first time, no problem occurs.
But on second time i get an error pop-up like "Not Unique in the id space of Window" and showing the list box item id which have to be filled on select.
Can any one help out there?
Note: Though it shows this error i get the listboxes filled correctly according to the combo box selection. Still i cant stop this error occurring..
Your issue is a conflict of ids in ZK's id space.
A bit of background..
ZK generates ids for components at runtime, if you open up the DOM in the browser you'll see each component has some machine readable id.
However, you can also give components an id. This id does not go to the DOM but lets you reference the component in your application.
These two shouldn't be confused. The conflict you're experiencing is with the latter type of id; you are assigning a component an id in your Java code or in your ZUL file which, at runtime, is not unique.
The case you describe where it only happens the second time you click is a tell tale sign here. The content you are adding on the event has an id defined in it and you are not removing this content when you are done.
Consider the following example:
#Wire
private Window myWindow;
#Listen(Events.ON_CLICK + " = #myButton")
public void onMyButtonClicked() {
Label myLabel = new Label("sean is cool");
myLabel.setId("myLabel");
myLabel.setParent(myWindow);
}
This will work the first time you click myButton, but will throw your error on the second click. That is because the second event tries to add myLabel to myWindow but there is already a myLabel there.
There are lots of ways to resolve this depending on what you are trying to do.
Have a look through the ZK documentation on ID Spaces for more.
I also faced the same error.
My scenario is the same, only the widgets being used are different.
Hence putting my workaround here.
I have put the following piece of code in the doBeforeCompose() method of the composer:
Component widgetWithId = page.getFellowIfAny("widgetWithId");
if (widgetWithId != null) {
widgetWithId.detach();
}
Here widgetWithId is the component/widget, which is tried to be regenerated by the code, with the same Id and ZK is throwing error for it.

Resources