Long live for a selenium script? - selenium

Let's say I have a webpage that I need to have some selenium script to automate the UI testing. This page has a list of sections, in the order of "Section A", "Section B", "Section C", etc. Here is my piece of code to automate the steps on "Section B" area testing.
by = By.CSS_SELECTOR
dropdown_in_SectionB = "#app > div.app > div > div > div.content > div:nth-child(2) > div > div:nth-child(7) > ..."
WebDriverWait(self.driver, 20).until(EC.presence_of_element_located((by, target))).click()
Unfortunately, I can not find a XPATH value for this "drowdown_in_SectionB" area, and I have to use css_selector value here. And it works ok for this second.
But later on, this page has been updated, the updated page now contains "Section A", "SectionX", "Section B", "Section C", in such order. And my above script is broken, as the css_selector value of drowpdown_in_Section becomes:
dropdown_in_SectionB = #app > div.app > div > div > div.content > div:nth-child(2) > div > div:nth-child(8) > ..."
So I have to update the script to have it back to working, which is annoyed.
My question is how may I find a way that the script is smart enough to locate the location in its "css_selector" value, however the change is ?
If it was of XPATH value, I can easily have it solved, as I have other clues to trace the dropdown area in Section, but how this can be done in case of css_selector value ???
Thanks,
Jack

Related

Scrape text under div tag that is in quotes

Trying to scrape this part: "Lounge, Showers, Lockers"
https://i.stack.imgur.com/k5mzg.png
<div class="CourseAbout-otherFacilities more">
<h3 class="CourseAbout-otherFacilities-title">Available Facilities</h3> " Lounge, Showers, Lockers "
</div>
Website:
https://www.golfadvisor.com/courses/16929-black-at-bethpage-state-park-golf-course
response.css('.CourseAbout-foodAndBeverage.more::text').get() command returns " \n "
Thank you
There are three text elements in your target div (matched by your CSS expression):
<div class="CourseAbout-otherFacilities more">FIRST<h3
<h3 class="CourseAbout-otherFacilities-title">SECOND</h3>
</h3>THIRD</div>
By using .get() you're telling Scrapy to return first match.
I recommend to use XPath expression here instead and match your element by text:
//h3[.="Available Facilities"]/following-sibling::text()[1]'

Extracting data from div tag

so im scraping data from a website and it has some data in its div tag
like this :
<div class="search-result__title">\nDonald Duck <span>\xa0|\xa0</span>\n<span class="city state" data-city="city, TX;city, TX;city, TX;city, TX" data-state="TX">STATENAME, CITYNAME\n</span>\n</div>,
I want to scrape "Donald Duck" part and state and city name after rel="nofollow"
the site contains a lot of data so name and state are different
the code that i have written is
div = soup.find_all('div', {'class':'search-result__title'})
print (div.string)
this gives me a error
"ResultSet object has no attribute '%s'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?" % key
first, use .text. Second, find_all() will return a list of elements. You need to specify the index value with either: print (div[0].text), or since you will probably have more than 1 element, just iterate through them
from bs4 import BeautifulSoup
html = '''<div class="search-result__title">\nDonald Duck <span>\xa0|\xa0</span>\n<span class="city state" data-city="city, TX;city, TX;city, TX;city, TX" data-state="TX">STATENAME, CITYNAME\n</span>\n</div>'''
soup = BeautifulSoup(html, 'html.parser')
div = soup.find_all('div', {'class':'search-result__title'})
print (div[0].text)
#OR
for each in div:
print (each.text)

Robot Framework for loop - selector always returning same element

I have an unordered list, from which I need to get the title from each element.
li class="chart-flyout-option" ng-repeat="option in filteredOptions = ($ctrl.options | filter: { value: $ctrl.filterText })" ng-class="{'chart-flyout-option-selected': option.key == $ctrl.selectedOption.key}"><a class="chart-flyout-link" ng-bind-html="option.value | highlight:$ctrl.filterText" title="HERE IS TITLE 1" ng-click="$ctrl.select(option.key, $event)" data-key="126">HERE IS TITLE 1</a></li>
Next element would be similar, but assume - HERE IS TITLE 2
I have a for loop, which looks like this:
Get Flyout Entry Child Links
${numflyoutentries}= Get Number Chart Flyout Entries
${numflyoutentries}= Evaluate ${numflyoutentries} + ${1}
: FOR ${entry} IN RANGE 1 ${numflyoutentries}
\ ${label}= Get Text class:chart-flyout-link
\ Log ${label}
However this only ever returns the text "HERE IS TITLE 1".
Why doesn't my selector increment?

Excel VBA Select Option Website Dropdown List

I am trying to automate this website using VBA excel. I am stuck at one point where I need to select value from the drop-down box. I am very much new to this as this is my first such project.
This is what I have coded to select the value:
Set objSelect = objIE.document.getElementById("personTitle")
For Each opt In objSelect.Options
If opt.Value = "Miss" Then
'Debug.Print "found!"
opt.Selected = True
'opt.Selected = "selected"
Else
'Debug.Print "not found!"
opt.Selected = False
End If
Next
I have also tried using the debug.print to check if the value that I am trying to find is actually getting matched or not- and it turns out that it matches.
The only problem I am facing is that the value is not getting set.
Can any of the gurus here please help?
Here is the HTML of that section:
<div class="input-wrap input-wrap__inline">
<div tabindex="-1" class="select is-placeholder"><div class="select_display">Title</div><div class="select_arrow glyphicon glyphicon-chevron-down"></div><dl class="select_list"><dt class="pretend-dd is-hover" data-index="1" data-val="Mr">Mr</dt><dt class="pretend-dd" data-index="2" data-val="Mrs">Mrs</dt><dt class="pretend-dd" data-index="3" data-val="Miss">Miss</dt><dt class="pretend-dd" data-index="4" data-val="Ms">Ms</dt><dt class="pretend-dd" data-index="5" data-val="Dr">Dr</dt></dl></div><select name="personTitle" class="parsley-validated hasCustomSelect .no-change, .bv-dropdown-select is-invisible" id="personTitle" required="" data-required-message="Please select a title">
<option selected="selected" value="">Title</option>
<option value="Mr">Mr</option>
<option value="Mrs">Mrs</option>
<option value="Miss">Miss</option>
<option value="Ms">Ms</option>
<option value="Dr">Dr</option>
</select>
</div>
I think you want a different class. The class in that HTML snippet is select_list. Then the subsequent dt tags.
If you observe the following CSS selector, where "." means class and " dt" means select all dt tags inside elements of that class, you will see it makes the correct selections:
In the code below, I translate this selector into:
ieDoc.getElementsByClassName("select_list")(0).getElementsByTagName("dt")
This assumes that index 0 is the correct one to use for elements of the class "select_list". You can easily inspect the collection to find the right index if you set it to a variable e.g.
Dim x As Object
Set x = ieDoc.getElementsByClassName("select_list")(0).getElementsByTagName("dt")
Code:
Dim currentOption As Object
For Each currentOption In ieDoc.getElementsByClassName("select_list")(0).getElementsByTagName("dt")
If InStr(currentOption.innerText, "Miss") > 0 Then
currentOption.Selected = True
End If
Next currentOption
Here are a couple options to try if you haven't already:
If opt.Value = "Miss" Then
'Debug.Print "found!"
opt.Click
OR
If opt.Value = "Miss" Then
'Debug.Print "found!"
opt.Focus
opt.FireEvent ("onchange")
If this turns out to be something done in kendoGrid or kendoDropDownList, I might be able to help with that also.

openerp page numbers

I have tried to add page numbers in my document using the following two ways
I just want default page 1, page 2 etc.
1)
<para>
<drawCentredString x="18.5cm" y="1.5cm"> Page: <pageNumber/></drawCentredString></para>
2)
<para><drawCentredString x="18.5cm" y="1.5cm"> Page: <pageCount/></drawCentredString></para>
for option 2, I changed the pagecount class in trml2pdf.py as below
class PageCount(platypus.Flowable):
def draw(self):
self.canv.beginForm("pageCount")
self.canv.setFont("Helvetica", utils.unit_get(str(8)))
## self.canv.drawString(0, 0, str(self.canv.getPageNumber()))
self.canv.drawString(0, 0, str(self.canv._pageCount))
self.canv.endForm()
class PageReset(platypus.Flowable):
def draw(self):
self.canv._pageNumber = 0
--
No Luck !! I just get page : or or
error
Thanks in advance,Usha
You can do so by just writing "< pageNumber/>" in your .rml, but you have to write it under "template/paggeTemplate/pageGraphics" tags.
For example:
<template pageSize="(595.0,842.0)" title="Test" author="Martin Simon" allowSplitting="20">
<pageTemplate>
<pageGraphics>
<drawCentredString x="10.5cm" y="0.8cm">Page: <pageNumber/></drawCentredString>
</pageGraphics>
</pageTemplate>
Hope this will solve your problem.
For the report with header = internal , you will have the 1/2 style page Count in report automatically .
as in report of openerp it is using Numbered canvasing for the internal Heard report .