how to select nth item from a CSS selector - selenium

[data-short-caption="itemName" i] .circle-base,
this selector is identifying two items in the DOM, I need to select the second item, is there any way like we have in xpath to select the second item ?
The HTML Structure is something like this :
<div class="selection" data-select-item="select-item">
<div data-short-caption='itemName'>
<div class=circle-base> </div>
</div>
<div data-short-caption='itemName'>
<div class=circle-base> </div>
</div>
</div>

As per the HTML:
<div class="selection" data-select-item="select-item">
<div data-short-caption='itemName'>
<div class=circle-base> </div>
</div>
<div data-short-caption='itemName'>
<div class=circle-base> </div>
</div>
</div>
To identify only the second item you can use either of the following css-selectors based Locator Strategy:
Using nth-child():
div.selection div:nth-child(2) > div.circle-base
Using nth-of-type():
div.selection div:nth-of-type(2) > div.circle-base

If I could correctly understand your query, there are several ways to achieve it via css pseudo-selectors. Please check the code if it helps.
/*Method 1 : using last-of-type pseudo-selector*/
[data-short-caption="itemName" i]:last-of-type .circle-base{
background-color: #efefef;
}
/*Method 2 : using last-child pseudo-selector*/
[data-short-caption="itemName" i]:last-child .circle-base{
background-color: #efefef;
}
/*Method 3 : using nth-of-type pseudo-selector*/
[data-short-caption="itemName" i]:nth-of-type(2) .circle-base{
background-color: #efefef;
}
/*Method 4 : using nth-child pseudo-selector*/
[data-short-caption="itemName" i]:nth-child(2) .circle-base{
background-color: #efefef;
}
<div class="selection" data-select-item="select-item">
<div data-short-caption='itemName'>
<div class=circle-base> first child </div>
</div>
<div data-short-caption='itemName'>
<div class=circle-base> second child </div>
</div>
</div>

Related

Why am I not able to scrape just this particular P tag?

I am using scrapy shell just to make sure my selectors for my spider are correct. I am able to get all other sections I need except this one p tag that contains the cross ref part numbers. I am scraping from this particular page here
When I try response.css('div.col-1-2-2' > div.rpr-help m-chm > div > p::text').extract() it returns blank
When I try response.css('div > p::text').extract() the results have the section I am looking for plus a bunch of data I do not want.
I have a feeling this is going to be a super easy answer, but I have no idea what I am missing here
This is a snippet of the html section of the page I am trying to scrape, the last 'p' tag starting with Part Number
<div class="col-1-2-2">
<div id="img-detail" style="text-align:center;">
<div id="img-detail-main">
<a id="ctl00_cphMain_imgenlarge" rel="nofollow" href="/detail-img.aspx?id=3094537&i=" class="cboxElement"><img id="ctl00_cphMain_iMain" src="https://cdn.appliancepartspros.com/images/product/cache/whirlpool-clutch-assembly-285785-ap3094537_01_l.jpg" style="border-width:0px;outline:none;">
<div class="img-overlay" style="display:none;"><img src="/images/play.png" style="height:107px;"></div>
<div id="main-text-overlay" style="display:none;"></div>
</a>
</div>
<div class="img-help">Click image to open expanded view</div>
<div id="img-detail-thumb">
<div class="a-button a-active">
<img id="ctl00_cphMain_rImgTh_ctl01_imgTh" src="https://cdn.appliancepartspros.com/images/product/cache/whirlpool-clutch-assembly-285785-ap3094537_01_tt.jpg" style="border-width:0px;">
</div>
<div class="a-button">
<img id="ctl00_cphMain_rImgTh_ctl02_imgTh" src="https://cdn.appliancepartspros.com/images/product/cache/whirlpool-clutch-assembly-285785-ap3094537_02_tt.jpg" style="border-width:0px;">
</div>
<div class="a-button">
<img id="ctl00_cphMain_rImgTh_ctl03_imgTh" src="https://cdn.appliancepartspros.com/images/product/cache/whirlpool-clutch-assembly-285785-ap3094537_03_tt.jpg" style="border-width:0px;">
</div>
<div class="a-button">
<img id="ctl00_cphMain_rImgTh_ctl04_imgTh" src="https://cdn.appliancepartspros.com/images/product/cache/whirlpool-clutch-assembly-285785-ap3094537_04_tt.jpg" style="border-width:0px;">
</div>
<div class="a-button">
<img id="ctl00_cphMain_rImgTh_ctl05_imgTh" src="https://cdn.appliancepartspros.com/images/product/cache/whirlpool-clutch-assembly-285785-ap3094537_05_tt.jpg" style="border-width:0px;">
</div>
<div class="a-button">
<img id="ctl00_cphMain_rImgTh_ctl06_imgTh" class="diagram" data-dcmt="Clutch assembly AP3094537 is number 5 on this diagram. This is to give you an idea of the appearance and the location of the part. Your appliance model may be slightly different." src="https://483cda5f439700fab03b-6195bc77e724f6265ff507b1dc015ddb.ssl.cf1.rackcdn.com/0029384112_4.gif" style="border-width:0px;">
</div>
<div class="a-button">
<img id="ctl00_cphMain_rImgTh_ctl07_imgTh" class="video" src="https://img.youtube.com/vi/7RS1l6t8efc/hqdefault.jpg" style="border-width:0px;">
<div class="img-overlay"><img src="/images/play.png"></div>
</div>
</div>
</div>
<div class="rpr-help m-chm">
<div class="header">
<h2 class="h6">Repair Help</h2>
</div><!-- /end .header -->
<div class="inner m-bsc">
<ul>
<li>Repair Video</li>
<li>Repair Q&A</li>
</ul>
</div>
<div>
<br>
<span class="h4">Cross Reference Information</span><br>
<p>Part Number 285785 (AP3094537) replaces 2670, 285331, 285380, 285422, 285540, 285761, 285785VP, 3350015, 3350114, 3350115, 3351342, 3351343, 387888, 388948, 388949, 3946794, 3946847, 3951311, 3951312, 62699, 63174, 63765, 64176, AH334641, EA334641, J27-662, LP326, PS334641.
<br>
</p>
</div>
</div>
</div>
Hope this works
response.xpath('//div[#class="col-1-2-2"]//p/text()').extract_first()
You can try this also, response.xpath('(//div[#class="rpr-help m-chm"]//p//text())[1]').get()

know number of elements with vba scraping web

hello everyone I would like to know numer of elements like the following
<div id="datatable">
<form id="theForm" name="theForm" >......</form>
<div class="no_data_dd" id="no_data" >....
<div class= ....>.....
<div class= ....>
</div>
</div>
</div>
<div class="score_row score_header">.../div>
<div class="score_row match_line e_true" >..</div>
<div class="score_row padded_date ">..</div>
<div class= ....>.....</div>
</div>
I tried with
Set itemEle = objIE.document.getElementById("scoretable")
Length = itemEle.getElementsByTagName("class").Length
length = 0 and nont =5
why?
What if you try get "elements" by instead of get "element" that might be a reason for the answer

Clarification on relative xpath

I want an xpath which represents the element 2 w.r.t element 1, as shown in the picture below. Can I build it using following-sibling?
Any help appreciated.
<div id="leads_data" class="uk-width-large-8-12 uk-container-center">
<div class="md-card-list leads_cards_list">
<ul class="">
<li class="item-shown" style="margin: 40px -20px; min-height: 90px;">
<div class="md-card-list-item-menu" data-uk-dropdown="{mode:'click',pos:'bottom-right'}">
<span>12:35:22 PM</span>
<a class="md-btn md-btn-flat md-btn-wave waves-effect waves-button" onclick="showModal(446)" data-uk-modal="{ center:true }" href="#lead_info">Edit</a>
</div>
<div class="md-card-list-item-sender">
<span>
<i class="material-icons">face</i>
CDYCUQYGNKYNFNFCWOUO
</span>
</div>
<div class="md-card-list-item-sender">
<span>
<i class="material-icons">call</i>
+91-1119771441
</span>
</div>
<div class="md-card-list-item-subject">
<span>
<i class="material-icons">email</i>
rnkntsoorh#jd.com
</span>
</div>
<div class="md-card md-card-list-item-content-wrapper uk-margin-top" onclick="showModal(446)" style="display: block; opacity: 1;">
<div class="md-card-content leads_card_content">
<div class="uk-text-large uk-margin-remove">
<b>Remarks:</b>
Some remarks of TSYNTORSAC
</div>
</div>
</div>
<br/>
</li>
I want to start with div having the text "CDYCUQYGNKYNFNFCWOUO" and locate the div having the text "Some remarks of TSYNTORSAC".
You can use below xPath :-
//span[contains(.,'CDYCUQYGNKYNFNFCWOUO')]/parent::div/following::div[contains(.,'Some remarks of TSYNTORSAC')][last()]
or
//span[contains(.,'CDYCUQYGNKYNFNFCWOUO')]/parent::div/following::div[contains(.,'Some remarks of TSYNTORSAC') and contains(#class, 'uk-text-large uk-margin-remove')]
or
//span[contains(.,'CDYCUQYGNKYNFNFCWOUO')]/parent::div/following-sibling::div[contains(.,'Some remarks of TSYNTORSAC')]/descendant::div[contains(#class, 'uk-text-large uk-margin-remove')]
or
//span[contains(.,'CDYCUQYGNKYNFNFCWOUO')]/parent::div/following-sibling::div[contains(.,'Some remarks of TSYNTORSAC')]/div/div
Hope it will help you..:)
In addition to the ways described by Saurabh gaur you can also use following-sibling to locate the desired element.
.//span[text()='CDYCUQYGNKYNFNFCWOUO']/../following-sibling::div[position()=3]//div[contains(.,'Some remarks of TSYNTORSAC')]
or
.//span[text()='CDYCUQYGNKYNFNFCWOUO']/../following-sibling::div[position()=3]/div/div[contains(text(),'Some remarks of TSYNTORSAC']
Hope this helps
You could simply get the element containing the desired text instead of using following:
//li[div[normalize-space(.)='face CDYCUQYGNKYNFNFCWOUO']]/div[last()]
try this :-
//div[contains(#class,'md-card-list-item-sende') and contains(.,'CDYCUQYGNKYNFNFCWOUO')]/following::div[contains(#class,'md-card-list-item-content') and descendant::b[contains(.,'Remarks')]]

Autocomplete with Selenium

I'm having some (actually, a lot) of trouble automating selection from autocomplete options with Selenium. Currently, I am able to automate the inputting of the text, though, I am not able to select anything from the appearing drop-down suggestions list that pops up. I tried searching here for some answers to my problem, but nothing has worked. Below is the element that appears with the suggestions that I am trying to select:
<div class="cs-autocomplete-popup">
<div class="inner">
<div class="cs-autocomplete-Matches csc-autocomplete-Matches">
<ul>
<li class="cs-autocomplete-matchItem csc-autocomplete-matchItem">
<span class="csc-autocomplete-matchItem-content cs-autocomplete-matchItem-content" id="matchItem::matchItemContent">john doe</span>
</li>
</ul>
</div>
<div class="csc-autocomplete-addToPanel cs-autocomplete-addToPanel">
<hr>
<div class="content csc-autocomplete-addTermTo cs-autocomplete-addTermTo">Add "John Doe" to:</div>
<ul>
<li class="cs-autocomplete-authorityItem csc-autocomplete-authorityItem" id="authorityItem:">Local Persons</li>
</ul>
</div>
</div>
<div class="cs-autocomplete-popup-miniView csc-autocomplete-popup-miniView" style="top: 2px; left: 149px; display: none;"><div class="cs-miniView">
john doe
<div>
<span class="csc-autocomplete-popup-miniView-field1Label cs-autocomplete-popup-miniView-field1Label">b.</span>
<span class="csc-autocomplete-popup-miniView-field1 cs-autocomplete-popup-miniView-field1" id="field1"></span>
</div>
<div>
<span class="csc-autocomplete-popup-miniView-field2Label cs-autocomplete-popup-miniView-field2Label">d.</span>
<span class="csc-autocomplete-popup-miniView-field2 cs-autocomplete-popup-miniView-field2" id="field2"></span>
</div>
<div>
<span class="csc-autocomplete-popup-miniView-field3 cs-autocomplete-popup-miniView-field3" id="field3"></span>
</div>
<div>
</div>
</div></div>
</div>
From this, I am trying to select "john doe". Does anyone know an efficient/complete way of doing this? I would very much appreciate the help.
driver.findElement(By.xpath("get the exact field address of autocomplete textbox");
Thread.sleep(5000);
//for xpath: need to take the common xpath from the list of the elements.
List<WebElement> link=driver.findElements(By.xpath("//span[contains(#class, 'cs-autocomplete-matchItem-content') and .='john doe']");
for(int i=0; i<=link.size(); i++)
{
if(link.get(i).getText().equalsIgnoreCase("john doe");
{
link.get(i).click();
}
}

Unable to get the values from <p> tag

I should get the values between the p tag.
Following is the code from which i need to get the values
<div id="ved-list-totals">
<div id="ved-sidebar-totals" class="clearfix margin-top-10" style="height: 100px;">
<div id="ved-sidebar-totals" class="clearfix margin-top-10" style="height: 100px; background-color: transparent;">
<div class="pull-right margin-left-20 margin-right-10 align-right">
<p class="no-margin">0</p>
<p class="no-margin" style="background-color: transparent;">5.97</p>
<p class="no-margin">0.00</p>
<p class="no-margin">4.95</p>
<p class="no-margin bold dark-text">10.92</p>
</div>
<div class="pull-right margin-left-20 align-right">
<p class="no-margin">Estimated Points</p>
<p class="no-margin">Subtotal</p>
<p class="no-margin">Tax</p>
<p class="no-margin">Service Fee</p>
<p class="no-margin bold dark-text">Estimated Total</p>
</div>
I have tried the following approaches:
String tot = driver.findElement(By.xpath("html/body/div[4]/table/tbody/tr/td[2]/div/div[1]/div[2]/div/div/div/div[2]/div/div/div[1]/p[2]")).getText();
String axd = driver.findElement(By.xpath("html/body/div[4]/table/tbody/tr/td[2]/div/div[1]/div[2]/div/div/div/div[2]/div/div/div[1]/p[3]")).getText();
String tot = driver.findElement(By.xpath("//div[#id='ecart-sidebar-totals']/div/p[4]")).getText();
String axd = driver.findElement(By.xpath("//div[#id='ecart-sidebar-totals']/div/p[3]")).getText();
I was getting the error as :
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//div[#id='ecart-sidebar-totals']/div/p[4]"}
There are two elements with the same id (<div id="ved-sidebar-totals">) should this id be blamed for this error, or is there any thing which am missing.
Use the xpath //div[#id='ved-list-totals']//p[1]. This should return the first p tag value with getText() method.
Wild guess..
//div[#id='ecart-sidebar-totals']/div[#id='ved-list-totals']/p[1]
Try using Css selector
driver.findElement(By.cssSelector("div[class='pull-right margin-left-20 margin-right-10 align-right'] p:nth-child(4)"));