How to handle div that is created dynamically in a table - selenium

I am trying to iterate a table to get the results. The structure of the table looks as mentioned in the picture
When I open the page, there is only one with role="presentation" and I am able to retrieve the data using the below css locator.
driver.findElement(By.cssSelector("div[id^=dojox_grid__View_] div.dojoxGridContent div.dojoxGridRow:nth-child(1) tbody tr:nth-child(1) td:nth-child(6)")).getText();
When I scroll the page manually another tag is created dynamically with role="presentation" and has many rows(div.dojoxGridRow). I want to iterate these rows too.
Selenium is able to go only to first level (first ) and get the details. I am not sure how to reach the second level

I'm not sure about which element you're trying to access.
But you can access to all div with 'presentation' role (return a list):
driver.findElements(By.cssSelector("div[role='presentation']"));
If you're trying to access to each row under div with role presentation:
driver.findElements(By.cssSelector(".dojoxGridRow"));
If you want to get rows child of div with role 'presentation':
List<WebElement> presentations = driver.findElements(By.cssSelector("div[role='presentation']"));
for (WebElement presentation : presentations) {
List<WebElement> rows = presentation.findElements(By.cssSelector(".dojoxGridRow"));
// DO SOMETHING...
}
Hope that helps.

Related

How to set limit scrolling page while scrapping instagram?

scrolldown=driver.execute_script("window.scrollTo(0, document.body.scrollHeight);var scrolldown=document.body.scrollHeight;return scrolldown;")
match=False
while(match==False):
last_count = scrolldown
time.sleep(3)
scrolldown = driver.execute_script("window.scrollTo(0, document.body.scrollHeight);var scrolldown=document.body.scrollHeight;return scrolldown;")
if last_count==scrolldown:
match=True
I want to scrape data from an Instagram profile with Selenium, but I don't know how to set the limit for scrolling the page. Because of the code above, the page keeps scrolling until I don't know when it stops. I just want to scroll through that account's posts until I find the one I'm looking for. 
As you mentioned "to scroll through that account's posts until I find the one I'm looking for" presumably the specific element should be having an unique attribute either among:
id
classname
aria-label
innerText
or can be identified uniquely within the HTML DOM with combination of it's attributes. Once you are able to construct the locator strategy which identifies the element uniquely, you can easily use scrollIntoView() method as follows:
element = driver.find_element(By.XPATH, "//unique_xpath_locator")
driver.execute_script("return arguments[0].scrollIntoView();", element)
Probably the best and safest way to scroll is to use
element = driver.find_element(...)
driver.execute_script('arguments[0].scrollIntoView({block: "center", behavior: "smooth"});', element)
this command scrolls smoothly in such a way that element is vertically at the center of the page. So in your case I suggest to scroll to the oldest loaded post (it should be located at the bottom of the screen) so that new ones are loaded, and repeat the process until you find the post you are looking for. You can do this with the following code
while 1:
loaded_posts = driver.find_elements(By.CSS_SELECTOR, 'article > div > div > div > div')
# scroll to last loaded post
driver.execute_script('arguments[0].scrollIntoView({block: "center", behavior: "smooth"});', loaded_posts[-1])
post_found = ...
if post_found:
break

How to identify individual controls within a search result using Selenium Webdriver?

I have a list of search results in the following link and would like to know on how can I identify the individual controls using dynamic xpath
http://www.bigbasket.com/cl/fruits-vegetables/?nc=nb
I'm able to get the list of product names displayed using the below line
List<WebElement> productResults = browser.findElements(By.xpath("//*[contains(#id,'product')]/div[2]/span[2]/a"));
I'm able to print the product names displayed in Page 1 using the below code, but however the list size is not matching with the list of results displayed in Page 1 so which I see blank lines in between when printing
System.out.println(productResults.size());
for(int i=0;i<productResults.size();i++){
System.out.println(productResults.get(i).getText());
}
Also I tried to locate the individual controls such as Qty text box, Add button in a similar like how I located the product names but the list count is not matching so which I cannot specify the quantity, add the required product to the cart.
Could you please help me with this one?
The first step is get only the visible itens (that is displayed), sou you can use this xpath:
"//*[contains(#id,'product')][not(contains(#style,'display:none'))]/div[2]/span[2]/a"
Now, you need to return the main iten div, that allows you to acess other functions. You can get the tag parents in this way:
"//*[contains(#id,'product')][not(contains(#style,'display:none'))]/div[2]/span[2]/a/../../.."
The elements that you recieve in this last XPath have all html itens that you want, as set quantity, select the dropdown etc. You can acess each using a findElement() in each IWebElement of the list. Example:
List<WebElement> productResults = browser.findElements(By.xpath("//*[contains(#id,'product')][not(contains(#style,'display:none'))]/div[2]/span[2]/a/../../.."));
for(WebElement element : productResults ){
IWebElement quantityInput = element.findElement(By.XPath("//input[contains(#id, '_qty')]"));
string quantityValue = quantityInput.getAttribute("value"); // if you want to know the current value. YOu can also parse it in an int
IWebElement addButton = element.findElement(By.XPath("//a[contains(#class, 'add-button')]"));
// etc to all elements inside element.
// Remember: Element is yout complete card of the item, that contains Value, name, image, buttons and all it.
}
Sorry for some Java syntax error. I am not a Java developer / tester. My piece of cake is C#.

Locating images uniquely when img ids are dynamic and src same for multiple images

I have 3 custom dropdowns which open when clicked on "down arrow" image appended at the end, Code for the image is something like :
<img id="x-auto-2017" class="x-form-trigger x-form-trigger-arrow "
src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==">
Image id are dynamic and are in order of like 2017, 2018 etc. So I cannot use contains and identify them uniquely.
I want to get them clicked one by one and select value from the dropdown. Please help how to identify them uniquely.
Below code should work for you:
List<WebElement> elements = driver.findElements(By.xpath("//img[contains(#class,'x-form-trigger x-form-trigger-arrow']"));
System.out.println("Number of drop downs on the page: " + elements.size());
for (WebElement ele : elements) {
ele.click();
//Do what ever you want
}
Thanks for your response, I used the position qualifier [N] as suggested by #Marcus, though have to hit and trial a bit but writing manual xpath on Chrome console I got the following answer to my query.
driver.findElement(By.xpath("(//img[starts-with(#id,'x-auto-2')])[2]")).click();

how can i delete a div that i created with dojo?

i create a div with heading and UL inside with dojo.create. how can i totaly delete this div with heading and UL children so that i can create a div with this id again (with different content)?
I create the div like this (i delete some attributes and the h1 and ul creation because it was too much code, i do it the same way like i create the div):
var newAlarmDiv = new dojox.mobile.ScrollableView({
id: "divAlarms",
dojoType: "dojox.mobile.ScrollableView",
});
newAlarmDiv.placeAt('mobileView','first');
i already tried it with the dojo.destroy command but when i create a new div after destroying it i get many different errors so it seems not to be deleted correctly.
How do i correctly "undo" the div creation?
greets
Tom
You are creating a "Widget", which has DOM elements associated with it. If you were destroying only dom elements you would use dojo.destroy.
Since you want to destroy the widget, you should use destroy or destroyRecursive. The API Docs have a good overview of what each method does.
var scrollableViewWidget = new dojox.mobile.ScrollableView({/*params*/});
scrollableViewWidget.placeAt('someDiv','first');
scrollableViewWidget.destroyRecursive();

How can I focus to a specific item which is in the bottom of the page in IDE

I am trying to select a specific item in a page which is at the bottom of the page. I want to verify that element is present and the same time I want to focus to that specific item.
How can I do this in the Selenium IDE?
I tried storeEval, but its specific co-ordinated which I don't want. I am looking for some dynamic command. I tried using css:.groupTile:contains("Concentrated") but the focus is not going to that particular item (Concentrated).
Can someone help me with Command, Target and value please?
CSS Selectors have many formats
i) Using id. Put this in Target: css=tag#id
tag = the HTML tag of the element being accessed,
id = the ID of the element being accessed
ii) Using class. Put this in Target: css=tag.class
tag = the HTML tag of the element being accessed,
class = the class of the element being accessed
In value you enter name of the item.