Chrome Driver: where is dynamic div element? - selenium-chromedriver

I'm trying to scrape the price from this page: https://www.samsclub.com/sams/mm-pinto-beans-12lb/prod21002291.ip?xid=plp:product:1:2
The price is in a div that I can see in the element using Dev Tools but it's not in the page source so my research tells me that it's probably a dynamically generated div element. More reading recommended Chrome Driver which returns the data with the dynamic div element but I'm finding it's not there. It's just like viewing page source from the browser.
ChromeDriver driver = new ChromeDriver(#"C:\chromedriver");
driver.Url = "https://www.samsclub.com/sams/mm-pinto-beans-12lb/prod21002291.ip?xid=plp:product:1:2";
driver.Navigate();
var source = driver.PageSource;
var pathElement = driver.FindElementById("Price-group");
Is there another step for ChromeDriver to retrun the data with the dynamic divs?
Thanks

Price-Group is not an id. it is a class. Try with FindElementByClassName. Hope that club value is selected before findind the price. Coz, I see price value is displayed only fater selecting club
var pathElement = driver.FindElementByClassName("Price-group");

Related

Setting the content of a span element,populated from a list using selenium

I am trying to set the content of a span element using selenium but seems it is not getting set as the value. The span field is a value which is filled in from a list as the user types in.
Further details on the problem
1)Go to http://www.goeuro.es.
2)Tried setting the destination field as under:
a)If the user types berlin in the destination field,it gets replaced by the text (this is getting populating from their js)
Berlín Alemania
I tried setting this using the following code.
#FindBy(xpath = ".//*[#id='$desktopSearchform']/div[1]/div[2]/d-departure-position/div/div/div/span[1]")
private WebElement fromSearchFieldSpan1;
#FindBy(xpath = ".//*[#id='$desktopSearchform']/div[1]/div[2]/d-departure-position/div/div/div/span[2]")
private WebElement fromSearchFieldSpan2;
.
.
.
.
((JavascriptExecutor)driver).executeScript("arguments[0].innerHTML = 'Berlín'", fromSearchFieldSpan1);
((JavascriptExecutor)driver).executeScript("arguments[0].innerHTML = 'Alemania'", fromSearchFieldSpan2);
When I tried with the above provided code,it seemed as it set the text as the title of the said fields as I was getting a input validation error upon submitting the page(as if these values were not entered at all).
Screenshot attached.
I can't set the value of this field using sendKeys() since this is not a text field.
Any thoughts how this can be set using selenium web driver.?
You're trying to handle span element while you should handle input. Try below code:
#FindBy(xpath = "//input[#id='$city']")
private WebElement fromSearchFieldSpan;
fromSearchFieldSpan.sendKeys('Berlin Alemania')

how to get the value of width shown in html tag using selenium and python

I want to get the width % shown. So that i can monitor the progress.How to get the value using selenium in python.
I don't know how to achieve this.
You can try the size call in the Webelement of Webdriver.
size Returns the size of the element
Example:
driver = webdriver.Firefox()
e = driver.find_element_by_class_name("progress-image")
size = e.size
print(size)
From documentation http://selenium-python.readthedocs.org/en/latest/api.html#module-selenium.webdriver.remote.webelement
Assuming that your class "progress-image" is unique and used only once for mentioned div as per your shared html code in comment.
Following will get width of mentioned class div :
element = driver.find_element_by_class_name("progress-image")
size = element.size
print(size)
You can fetch the attribute style's value using get_attribute method and then use split method to split the resultant string and get the width value as #Vivek has suggested.
Also, since this is a progress-bar, as you say, then try waiting for its visibility first, using explicit wait and then fetch the attribute, as below:
wait = WebDriverWait(driver, 20)
element = wait.until(EC.visibility_of_element_located((By.CLASS_NAME,'progress-image')))
element_width = element.get_attribute('style').split(';')[0].split(':')[1]
print(element_width)
NOTE: The above code waits for 20 seconds till the visibility of the progress-bar element is located. Then, it fetches the style attribute of the progress-bar element and then splits it to fetch the required width value.
Furthermore, as mentioned in get_attribute method,
the value of the attribute so fetched may differ across browsers
(this is the case for the “style” attribute, for example)
EDIT
To wait till the upload is complete:
//waiting 3 minutes till the upload the uploader status has the title 'Click to close.'
wait = WebDriverWait(driver, 180)
element = wait.until(EC.visibility_of_element_located((By.XPATH,"//div[#class='uploader-status' and contains(#title, 'close')]")))

How to retrieve data from the Content Picker with Razor?

I try to use the Content picker w/o success..
I created a doc type which the properties are Content pickers, so what I try to do is along the magazine to use this page to pick up the features news / etc and display them on home page. w/o success..
I used something like:
dynamic feature_news_item = Library.NodeById(1111); (tried also the same but with Model: Model.NodeById(1111); Let's say for the example that 1111 is the id of this page which store the content pickers.
So again, the property of the page is "Content Picker" and the alias is featureItemNews
Now, I try to get / display the ID which the Content Picker (as much as I know..) should retrieve but just get errors (or nothing...) - tried this for example:
var node = #Library.NodeById(feature_news_item.contentPicker);
#node.Name
Doesn't show the Url nor the Name
So I tried
var node = #Library.NodeById(feature_news_item.featureItemNews);
int story1 = #feature_story.featureItemNews
Nothing works
Again featureItemNews is the Alias
and feature_news_item is just to get the Node with the Content picker (see at the beginning of the question)
All I try now is just to get the ID of the selected node which store in the content picker and display it / use it
What's the magic?
Thanks a lot for your help!
I've added Content Picker with alias (and name): myTestContentPicker
In razor code
:
var current = umbraco.NodeFactory.Node.GetCurrent();
int id = current.GetProperty<int>("myTestContentPicker");
< h1>#id< /h1>
It shows the ID of node I have selected. Then I can do whatever I want with this node:
var node = new Node(id);

Copy in contenteditable <span>

I need to copy content in span tag <span contenteditable="true">//content</span>
but it is not implemented in FireFox, is there any solutions for this?
and this is my span http://jsfiddle.net/watxD/
A. var Result = $('span[contenteditable="true"]').text();
B. var Result = $('span[contenteditable="true"]').html();
C. List of nodes inside: var Result = $('span[contenteditable="true"]').contents();
D. You can do it with Rangy Library http://code.google.com/p/rangy/. (It use native methods for FF, Chrome, Opera, IE9 and not-native for IE <= 8 )
Code below is using jQuery - but you can rewrite it on pure node Javascript
You need to create range for you node
var Range = rangy.createRange();
You need to select content of node
Range.selectNodeContents( $('span[contenteditable="true"]')[0] )
Represent result as you want:
var Result = Range.toString(); // Returns the text contained within the range.
var Result = Range.toHtml(); // Returns a string containing an HTML representation of the range.
There isn't pure JavaScript way to do this in Firefox without requiring the user to mess around with their preferences. Allowing general access to the system clipboard from JavaScript is dangerous, so the user may not want to do this. Here's an article that outlines the issues and how to enable clipboard access in Firefox. I don't know whether it's up to date.
http://kb.mozillazine.org/Granting_JavaScript_access_to_the_clipboard
The other option is a Flash-based hacky workaround such as ZeroClipboard.

Get page selection including HTML?

I'm writing a Chrome Extension, and I was wondering if it was possible to get the selected text of a particular tab, including the underlying HTML? So if I select a link, it should also return the <a> tag.
I tried looking at the context menu event objects (yes, I'm using a context menu for this), and this is all that comes with the callback:
editable : false
menuItemId : 1
pageUrl : <the URL>
selectionText : <the selected text in plaintext formatting, not HTML>
It also returns a Tab object, but nothing in there was very useful, either.
So I'm kind of at a loss here. Is this even possible? If so, any ideas you might have would be great. Thanks! :)
Getting the selected text of a page is fairly easy, you can do something like
var text = window.getSelection().toString();
and you'll get a text representation of the currently selected text that you can pass from a content script to a background page or a popup.
Getting HTML content is a lot more difficult, mostly because the selection isn't always at a clean HTML boundary in the document (what if you only select a small part of a long link, or a few cells of a table for example). The most direct way to get all of the html associated with a selection is to reference commonAncestorContainer, which is a property on a selection range that corresponds with the deepest node which contains both the start and end of the selection. To get this, you'd do something like:
var selection = window.getSelection();
// Only works with a single range - add extra logic to
// iterate over more ranges if needed
var range = selection.getRangeAt(0);
var container = range.commonAncestorContainer;
var html = container.innerHTML
Of course, this will likely contain a lot of HTML that wasn't actually selected. It's possible that you could iterate through the children of the common ancestor and prune out anything that wasn't in the selection, but that's going to be a bit more involved and may not be necessary depending on what you're trying to do.
To show how to wrap this all up into an extension, I've written a short sample which you can reference:
http://github.com/kurrik/chrome-extensions/tree/master/contentscript-selection/
If you don't want all of the siblings, just the selected HTML, use range's other methods like .cloneContents() (to copy) or .extractContents() (to cut).
Here I use .cloneContents():
function getSelectedHTML() {
var range = window.getSelection().getRangeAt(0); // Get the selected range
var div = document.createElement("div");
div.appendChild(range.cloneContents()); // Get the document fragment from selected range
return div.innerHTML; // Return the actual HTML
}