I am automating some things for work with Selenium and I am attempting to drag a drop a list in sorted order. So I have a list of the link names and am looping through so that for each item I can drag and drop the [drag] element to the top of the page.
I can't figure out how to grab that [drag] handle element using the link_text ("Audit: " in this case).
I've tried selecting the desired element with the link_text locator and then using .attribute("id") to get the id which should be the same for the link and for the [drag] handle. The problem is that I am getting a blank string for the id instead of "evaluation_422"
reversed.each do |form|
puts form
eval = browser.find_element(link_text: "#{form}")
id = eval.attribute("id")
puts id
#returns blank string
end
Any help is greatly appreciated! Below is the HTML I am trying to reference.
<li id="evaluation_422" class="evaluation">
<div class="right">
<span class="handle">[drag]</span>
<a href="/evaluations/422/edit">
<span>Audit: </span>
</a>
Okay so I finally solved this issue by grabbing the href of the link which happened to contain the number of the evaluation, so I was able to select the correct handle element using this:
reversed.each do |form|
href = browser.find_element(link_text: form).attribute("href")
eval_num = href.gsub("https://thisistheurl.com/evaluations/","").gsub("/edit","")
handle = browser.find_element(xpath: ".//*[#id='evaluation_#{eval_num}']/span")
top = browser.find_element(class_name: "evaluation")
browser.action.drag_and_drop(handle, top).perform
end
Related
Within the Moodle (v. 3.5.7) Atto editor (using both Chrome and Firefox) I've been trying to assign an ID to a particular row class, "span9". My ultimate objective is to assign this a unique ID and reference this element via jquery so as to append another element within it.
The ISSUE is that once I add an ID (id="checklist01") and click save, the ID simply does not appear in the DOM, and seems to not exist. When I re-enter the atto editor however, voila, there it is just sitting there. So it's NOT being removed completely... just not expressed somehow?
I have 2 screenshots linked below showing (1) the editor view, with the element and assigned ID highlighted, and (2) a screenshot of the DOM once the changes have been saved, with that same area highlighted, without the assigned ID.
Screenshots of ID Missing from DOM
Bootstrap ver. 4
So far I've tried switching the placement of the id in the atto editor (class coming first vs second after ); tried to add a "span" in front of the id (for some reason, I was desperate); and really just searched all over for someone who has encountered something similar.
I'm not sure how much help the html will provide, but here it is:
<div class="row-fluid colored">
<div class="iconbox span3">
h4>Your Completion Status (%)</h4>
</div>
<div id="checklist01" class="span9">
</div>
</div>
I found the reason for the removal of id attributes.
id attributes are removed because "Checklist" activity used safe HTML function of Moodle. If you want to access id attributes of description HTML follow below steps.
Go to mod\checklist\locallib.php file.
Then search formatted_intro() function (which is around line number 880).
In that function they used Moodle's format_text() function to return description text.
In that function, they have used 3 parameters.
string $text The text to be formatted.
int $format Identifier of the text format to be used
object/array $options text formatting options
Replace
$opts = array('trusted' => $CFG->enabletrusttext);
to
$opts = array('trusted' => $CFG->enabletrusttext,'allowid'=>true);
Then save your file and check. By following the above steps you can use id attributes.
Issue : Hey, I am trying, from VBA,to select multiple values in a dropdown list on a webpage.
The HTML code of the select element is
I managed to select this element with objIE.document.getElementsByTagName("select")(0) since it's the first element with the tagname "select" on the whole webpage.
If I write objIE.document.getElementsByTagName("select")(0).Value= "E31", the value "E31" will be highlighted in the drop box, that's fine.
But how can I select multiple values ?
I tried objIE.document.getElementsByTagName("select")(0).Value= "E31" + "E32" or objIE.document.getElementsByTagName("select")(0).Value= "E31" & "E32" but it doesn't work...
EDIT : Further attempts : I am currently trying to fix this issue using the SendKeys command where I click on one element of the list and then select other elements with SendKeys "+{DOWN 4}" which should select the 4 elements in the list under the initial one selected.
'objIE.document.getElementsByTagName("select")(0).Children(8).Selected = True' If I activate this line it selects (= highlight) the 9th element of the list
'objIE.document.getElementsByTagName("select")(0).Children(8).Click'if I activate this line it clicks on the 9th element of the list but doesn't highlight it
SendKeys "+{DOWN 3}"
Unfortunately this doesn't work yet. Moreover, I don't know if there is a way to send a key CTRL + MouseLeftClick somehow as mentioned in here but for the command Send which is not valid in VBA.
Any ideas ?
Solution : Ok I found a way to do it, pretty easy actually.
With objIE.document.getElementsByTagName("select")(0)
.Children(23).Selected = True
.Children(56).Selected = True
.Children(98).Selected = True
End With
This command will highlight (select) the 3 elements
Based off of an answer on here about a similar thing, I tried to scrape the text of Erowid trip experiences. The URL has a bunch of trip links. I want to click each link and then print the 'report-text-surround' element, which is the trip text.
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.erowid.org/experiences/exp.cgi?S1=2&S2=-3&C1=9&Str=')
#I tried to get hrefs by xpath, knowing that each trip links starts with 'exp.php?ID'.
view_links = driver.find_elements_by_xpath("""//*[contains(text(), 'exp.php?ID')]""")
for index, view in enumerate(view_links):
html = view.get_attribute('innerHTML')
href = html.split('"')[1]
view_links[index] = href
#And then visit each href and get the data
for href in view_links:
driver.get(href)
#I know this is the element containing the trip text.
trip_text = driver.find_elements_by_class_name('report-text-surround')
for trip in trip_text:
print (trip.text.encode('utf-8'))
So you are pretty close but there are just 2 small mistakes.
trip_text = driver.find_elements_by_class_name('report-text-surround')
for trip in trip_text:
print (trip.text.encode('utf-8'))
Your driver.find_elements_by_class_name should not be plural, as there is only one on the page. It has a lot of elements, but only one class ('report-text-surround'). This means you're going to get all the text at once, you could change this but you'd have to go through the child elements or get the elements seperately.
You can change that entire section to this:
text = (driver.find_element_by_class_name('report-text-surround').text).encode('utf-8')
print(text);
That will give you all of the text in the entire article. An easy way to split this up after would be to split each part of the text by \n\n.
I need to select the element labeled "Save" in the following HTML:
<td id="glActionAreaItem_0" tabindex="401" widgetid="glActionAreaItem_0">
<a class="actionItem" dojoattachevent="onclick:_onClick" tabindex="401" href="javascript:void(0);"> Reset </a>
</td>
<td id="ActionAreaItem_1" tabindex="402" widgetid="glActionAreaItem_1">
<a class="actionItem" dojoattachevent="onclick:_onClick" tabindex="402" href="javascript:void(0);"> Save </a>
</td>
I need to grab the Save object, but there are very few unique fields between the Reset and Save objects, and I don't want to rely on the tabindex cause we are not guaranteed that will stay 402 forever.
Is it possible to use the 'Save' label as the XPath? How would I go about doing that? Or perhaps I can use the id="glActionAreaItem_1" and somehow get that objects children, would that give me the Save object? Sorry, thinking out loud.
Dim btn_Save
'page is the HTML page
Set btn_Save = page.FindChildByXPath("//a[#tabindex='402']:, True)
I need another way to get the btn_Save object, not using the tabindex property because it is not going to be 402 always.
Here is another way to get the Save object.
Set lnk_Save = page.FindChildByXPath("//td[#id='glActionAreaItem_1']//a[#class='actionItem']", True)
Yes, you can select an element based on its string value in XPath.
This XPath will select the a element whose string value is "Save":
//a[normalize-space(.) = 'Save']
That way it won't matter whether #tabindex or any other attribute changes.
(normalize-space() will trim away leading and trailing whitespace and consolidate internal space (not applicable here).)
<a class="spf-link current" href="/orders/returns?offset=0&limit=25">
<span class="pg-helpText">Page</span>
1
</a>
I need to read the value '1' in my test. When I do a get text using css-selector .spf-link.current, it gets me "Page 1". I only need '1'. How do I exclude the text from the span tag.
You will need Javascript childNodes to get the text from the nodes.
The childNodes property returns a collection of a node's child nodes,
as a NodeList object.
The nodes in the collection are sorted as they appear in the source
code and can be accessed by index numbers. The index starts at 0.
WebElement element = driver.findElement(By
.cssSelector(".spf-link.current"));
String node_text=(String)((JavascriptExecutor)driver)
.executeScript("return arguments[0].childNodes[2].nodeValue",element);
This should give you the value 1 as node_text when you run the JavascriptExecutor. Other way is to split the text Page 1 on the basis of space. But, i would prefer childNodes.
Let me know if you have any queries.
Try the following:
//span[#class="pg-helpText" and contains(text(), 'Page')]/following-sibling::span/span
It is not normal to customize untagged text between tags.
I recommend you to change the value to your desired result:
<a class="spf-link current" href="/orders/returns?offset=0&limit=25">1</a>
...and when you want to show it in other place, do it as var ThePage = "Page " + [the page number]
Other solution is to include it into another tag as a div or as another span with a different class or id.