Roboframework Selenium - fidn web element and iterate in nested sub-elements - selenium

Hello I have this HTML structure:
<div class="1234" role="article">
<div class="A">
<h2 class="B">
<a class="C" href="https://www.test.it">
</a>
</h2>
</div>
<div class="X">
<span class="Y">"some text"
</span>
</div>
</div>
<div class="1234" role="article">
<div class="A">
<h2 class="B">
<a class="C" href="https://www.test2.it">
</a>
</h2>
</div>
<div class="X">
<span class="Y">"some text2"
</span>
</div>
</div>
My goal is to iterate in each Div with role=article, and gather corresponding href and text
(i.e. https://www.test.it - "some text" for the first one)
I've created a basic for loop:
${elements}= Get WebElements xpath://div[contains(#role, 'article')]
FOR ${element} IN #{elements}
Log To Console ${element.get_attribute('href')}
END
But i cannot figure it out how to get the sub elements that I need.
Any help is more than appreciated.
Many thanks
###Update
this works for the href, but I'm unable to get the span text
${elements}= Get WebElements xpath://div[contains(#role, 'article')]
FOR ${element} IN #{elements}
${sub1}= Set Variable ${element.find_element_by_xpath(".//h2//a[contains(#class, 'C')]")}
Log To Console ${sub1.get_attribute('href')}
END

To iterate in each DIV with role="article" and gather corresponding href and text you can use the following Locator Strategies:
href:
${elements}= Get WebElements xpath://div[contains(#role, 'article')]
FOR ${element} IN #{elements}
${sub1}= Set Variable ${element.find_element_by_xpath(".//h2/a[#class='C']")}
Log To Console ${sub1.get_attribute('href')}
END
text:
${elements}= Get WebElements xpath://div[contains(#role, 'article')]
FOR ${element} IN #{elements}
${sub1}= Set Variable ${element.find_element_by_xpath(".//div[#class='X']/span[#class='Y']")}
Log To Console ${sub1.get_attribute('innerHTML')}
END

Related

how to ignore span with List<WebElement>

I am trying to extract Main Text1 and Main Text2 from below DOM structure.
<div class="origination">
<div class="origin">
<h3>
Main Text1
<span class="info">Test1</span>
</h3>
<div class="test">
</div>
</div>
<div class="origin">
<h3>
Main Text2
<span class="info">Test2</span>
</h3>
<div class="test">
</div>
</div>
</div>
I have used below xpath with text() to identify header without span
#FindBy(xpath = ".//*[#id='origination']/div[*]/h3/text()")
List<WebElement> content;
I am trying to print header element without span using below code
for (WebElement element: content){
System.out.println("Header" + element.getText());
}
But I am getting error:
org.openqa.selenium.NoSuchElementException: Timed out after 15 seconds. List elements not found
However, when I used the same XPath on the firepath elements get highlighted in the DOM structure but not on to the page.
Here is the answer.
spanText = element.findElement(By.xpath("//span[#class='info']")).getText()
element.getText().Replace(spanText, string.Empty);

Unable to click an elemt " Search Intimation View-Details" thows not such element Exception

I am new to selenium,Guys please help me to click this element " Search Intimation View-Details".Cant able to use the ID has it is in number,& class name is not pointing exactly to that button.Guide me please,I'm strucked up.I tried
driver.findElementByXPath(" //div[span='Search Intimation View-Details'] ").click();
//
driver.findElementByClassName("v-tree-node v-tree-node-expanded v-tree-node-root v-tree-node-last ").click();
Below is the code
<div class="v-tree-node v-tree-node-expanded v-tree-node-last" id="gwt-uid-36" role="treeitem" aria-selected="false" aria-labelledby="gwt-uid-35" aria-level="2" aria-expanded="true">
<div class="v-tree-node-caption">
<div id="gwt-uid-35" for="gwt-uid-36">
<span>Intimations</span>
</div>
</div>
<div class="v-tree-node-children v-tree-node-children-last" role="group">
<div class="v-tree-node v-tree-node-leaf v-tree-node-leaf-last" id="gwt-uid-38" role="treeitem" aria-selected="true" aria-labelledby="gwt-uid-37" aria-level="3">
<div class="v-tree-node-caption v-tree-node-selected">
<div id="gwt-uid-37" for="gwt-uid-38">
<span>Search Intimation View-Details</span>
</div>
</div>
<div class="v-tree-node-children v-tree-node-children-last" role="group"></div>
</div>
</div>
</div>
I believe you are using Java selenium binding, so Use this code
driver.findElement(By.xpath("//span[normalize-space()='Search Intimation View-Details']")).click()
The below script uses the Java programming language and uses the CSS locators to find the required element if we are unable to find using the class, id, XPath, etc.
Use:
driver.findElement(By.cssSelector("div[role=treeitem][id^='gwt-uid-36']")).click();

Web scraping Linkedin Job posts using Python, Selenium & Phantomjs

Some LinkedIn job posts contain a see more button that expands the whole job description:
https://www.linkedin.com/jobs/view/401243784/?refId=3024203031501300167509&trk=d_flagship3_search_srp_jobs
I tried to expand it using the element.click() but the source I get after expansion contains some placeholder divs instead of the original div. How, can I scrap those hidden texts.
This is what I get from driver.page_source
<div class="jobs-ghost-placeholder jobs-ghost-placeholder--medium jobs-ghost-placeholder--thin mb2"></div>
<div class="jobs-ghost-placeholder jobs-ghost-placeholder--x-small jobs-ghost-placeholder--thin mb2"></div>
<div class="jobs-ghost-placeholder jobs-ghost-placeholder--small jobs-ghost-placeholder--thin"></div>
Instead of the source I get from chrome inspect:
<div id="ember7189" class="jobs-description-details pt5 ember-view"> <h3 class="jobs-box__sub-title js-formatted-exp-title">Seniority Level</h3>
<p class="jobs-box__body js-formatted-exp-body">Associate</p>
<!---->
<h3 class="jobs-box__sub-title js-formatted-industries-title">Industry</h3>
<ul class="jobs-box__list jobs-description-details__list js-formatted-industries-list">
<li class="jobs-box__list-item jobs-description-details__list-item">Real Estate</li>
<li class="jobs-box__list-item jobs-description-details__list-item">Information Technology and Services</li>
</ul>
<h3 class="jobs-box__sub-title js-formatted-employment-status-title">Employment Type</h3>
<p class="jobs-box__body js-formatted-employment-status-body">Full-time</p>
<h3 class="jobs-box__sub-title js-formatted-job-functions-title">Job Functions</h3>
<ul class="jobs-box__list jobs-description-details__list js-formatted-job-functions-list">
<li class="jobs-box__list-item jobs-description-details__list-item">Information Technology</li>
<li class="jobs-box__list-item jobs-description-details__list-item">Project Management</li>
<li class="jobs-box__list-item jobs-description-details__list-item">Product Management</li>
</ul>
</div>
I also tried different values for the wait WebDriverWait(driver, 3) but in vain.
code:
employment_type = wait.until(EC.presence_of_element_located(
(By.CSS_SELECTOR, 'div.jobs-description__details>div.jobs-description-details>p.js-formatted-employment-status-body'))).text
raises timeout exception as it only finds those jobs-ghost-placeholder instead of the described css_selector

How to Click on a Text in Selenium Webdriver 2.x

I am not able to click on the below HTML values through selenium webdriver click command through Java.
Here's my HTML...I have to click on PAAcctAcctRels, PAAcctActivityData, etc. as in the HTML.
I tried with LinkText (driver.findElement(By.linkText("PAAcctAcctRels")).click();) and xpath (driver.findElement(By.xpath(".//[#id='primaryNavLevel2Z6_G868H4S0K881F0AAEO37LG28N0']/div[1]/a")).click();)
<div id="primaryNavLevel2Z6_0G5A11K0KGF200AIUB98T20G52" class="dropdown_1columns">
<div class="col_1">
<a class="" href="?uri=nm:oid:Z6_0G5A11K0KGF200AIUB98T20G53">
<strong>
<span lang="en" dir="ltr">
PAAcctAcctRels
<span class="wpthemeAccess"> currently selected</span>
</span>
</strong>
</a>
</div>
<div class="col_1">
<a class="" href="?uri=nm:oid:Z6_0G5A11K0KGF200AIUB98T20GD4">
<span lang="en" dir="ltr">PAAcctActivityData</span>
</a>
</div>
<div class="col_1">
<a class="" href="?uri=nm:oid:Z6_0G5A11K0KGF200AIUB98T20GT1">
<span lang="en" dir="ltr">PAAcctAddrEmail</span>
</a>
</div>
Is there any other way to do this..please let me know.
1- For Clicking on text 'PAAcctActivityData', you can use the below code:
driver.findElement(By.xpath("//span[.='PAAcctActivityData']")).click();
2- For Clicking on text 'PAAcctAddrEmail', you can use the below code:
driver.findElement(By.xpath("//span[.='PAAcctAddrEmail']")).click();
NOTE:- The above xpaths will locate thespan elements with exact innerHTML/text as 'PAAcctActivityData' or 'PAAcctAddrEmail', respectively.
By.linkText("PAAcctAcctRels") won't work because that link has more text (ie ' currently selected'), and the problem with your xpath is that is starts with .//
The following should work (I have avoided using * for performance)
By.xpath("//div[#id='primaryNavLevel2Z6_G868H4S0K881F0AAEO37LG28N0']/div[1]/a")
Try using //[#id='primaryNavLevel2Z6_G868H4S0K881F0AAEO37LG28N0']/div[1]/a/span
as xpath. Remove the initial '.' and add '/span' at the end.

Locating element in a pagination bar

The button in the code "AS PDF" ,As excel" are wrapped in the button Export and the export button is embedded within a pagination toolbar
I am unable to locate this element in webdriver
tried
"//span[text()='As PDF']"
Still i see error unable to locate the element
<li class="leaf">
<button id="export" class="button capsule mutton up last over">
<span class="wrap">
Export
<span class="icon"/>
<span class="indicator"/>
</span>
</button>
</li>
ul id="menuList">
<li id="menuList_simpleAction.Report.exportReport" class="leaf">
<p class="wrap button">
<span class="icon"/>
<!--Item text goes here-->
As PDF
</p>
</li>
<li id="menuList_simpleAction.Report.exportReport" class="leaf">
<p class="wrap button">
<span class="icon"/>
<!--Item text goes here-->
As Excel
</p>
Please help
If you are using java code then you can use
driver.findElement(By.linkText("As PDF"));
or you can get all the icons using
List<WebElement> list = driver.findElements(By.xpath("//span[#class='icon']"));
for(WebElement ele: list){
if(ele.getText().equals("As PDF")){
ele.click();
}
}
Hope this helps you.