I have a list of li's within an ul, where the li would look like:
<li class="list-group-item operator">
<div class="pull-right">
<a href="/index.php?page=excursion_transport&sub_page=operators&mode=edit&id=10" class="tooltip-hook" title="" data-original-title="Rediger operatøren">
<span class="fa fa-pencil fa-fw"></span>
</a>
<span class="text-muted tooltip-hook" title="" data-original-title="Operatøren kan ikke slettes da den har transportmidler.">
<span class="fa fa-trash fa-fw"></span>
</span>
</div>
Ice Cap Tours
</li>
How do I with Selenium IDE find the text "Ice Cap Tours" and at the same time press the pencil that are present in the "span class='fa-pencil'"?
If you're looking to interact with them you'd need the following locators
//a[contains(text(),'Ice Cap Tours')]//..
//span[contains(#class, 'fa-pencil')]
The first will just look for the span containing the text you're after, and the "//.." navigates up to the parent element (the a tag) to click on the link
The second one will look for the span whos class contains "fa-pencil", you may have had trouble with the other suggested locators as they will have been looking for an exact match of the class, rather than just a class which contains that one
Use click or clickAndWait Command and in Target you can write xpath as //a[text()='Ice Cap Tours']//preceding-sibling::div/a[#class='fa-pencil']
You can try bellow xpath :
//a[normalize-space()='Ice Cap Tours']/preceding-sibling::div/a/span[contains(#class,'fa-pencil')]
Here is explanation :
//a[normalize-space()='Ice Cap Tours']
This will locate your anchor tag which having text Ice Cap Tours
/preceding-sibling::div
Used to navigate to locate the preceding sibling node of a tag. In your case div is preceding sibling of a tag
div/a/span[contains(#class,'fa-pencil')]
Will locate your span which having class name pencil
Related
Node v10.15.3 (64-bit)
Puppeteer v1.14.0
Chromium v 75.0.3738.0 (Developer Build) (64-bit)
I am at the very last page of a site automation tool and the last button I need to press (which is actually an anchor tag with an "id" attribute) cannot be found by Puppeteer. The code is:
await page2.click('#____bs');
The error I receive is:
Error: Node is either not visible or not an HTMLElement
The HTML code is here:
<a role="button" ct="Button" rel="tooltip" title="Post"
aria-label="Please Select Post" data-toggle="cso-button" id="____bq"
class="cso-btn cso-btn cso-action cso-corner cso-btn-primary "
href="javascript:void(0)"
data-ctl-options="{'actiontype';:'ACTION','value':'Click'}"
style="display: inline-block;">Post</a>
While in debug mode with this page up, I can use JQuery to find the element easily:
$("#____bq").text()
"Post"
Therefore, why can't Puppeteer find it? Is there an alternate way I could try to click this link?
Could it also be that the control (the anchor) is outside the visible edge of the screen? I wouldn't think that would matter since I'm assuming Puppeteer is looking at the HTML and not the screen itself but doesn't hurt to ask.
Thanks in advance.
UPDATE: Here's the entire HTML that contains the anchor I'm trying to select:
<div class="row split">
<div class="clsFP-expand clsBG-expand cso-hidden cso-cont-50r cso-pad5r">
<span class="cso-pad5-ie">
<a role="button" ct="Button" rel="tooltip" title="Cancel" aria-label="Please Select Cancel" data-toggle="cso-button" id="____bo" class="cso-btn cso-btn cso-action cso-corner cso-btn-grey " href="javascript:void(0)" data-ctl-options="{"actiontype":"ACTION","value":"Click"}" >Cancel</a>
</span>
<span class="cso-pad5-ie">
<a role="button" ct="Button" rel="tooltip" title="Post" aria-label="Please Select Post" data-toggle="cso-button" id="____bs" class="cso-btn cso-btn cso-action cso-corner cso-btn-primary inactive " href="javascript:void(0)" data-ctl-options="" >Post</a>
<a role="button" ct="Button" rel="tooltip" title="Post" aria-label="Please Select Post" data-toggle="cso-button" id="____bq" class="cso-btn cso-btn cso-action cso-corner cso-btn-primary " href="javascript:void(0)" data-ctl-options="{"actiontype":"ACTION","value":"Click"}" >Post</a>
</span>
</div>
</div>
It's not that Puppeteer is not being able to get the element. That's a validation coming from Puppeteer (see https://github.com/GoogleChrome/puppeteer/blob/5ee21d97e796263857b9f3fbeaf63366179d346f/lib/JSHandle.js#L203).
According to the doc:
This method fetches an element with selector, scrolls it into view if needed, and then uses page.mouse to click in the center of the element. If there's no element matching selector, the method throws an error.
That means that Chromium should be able to scroll to that element, and that element should be visible in order to be able to click on it.
HTML code:
<div id="routingPanel" class="">
<div id="routingPanelRight">
<ul id="routingList" class="ui-sortable">
<li class="ui-menu-item ui-draggable" style="display: list-item;" role="presentation" data-type="srl" data-id="15">
<a class="ui-corner-all" tabindex="-1">AS-HTTS-US-LAN-SW</a>
<span class="fa fa-trash"/>
<span class="type">[srl]</span>
</li>
<li class="ui-menu-item ui-draggable" style="display: list-item;" role="presentation" data-type="queue" data-id="119">
<a class="ui-corner-all" tabindex="-1">AS-EMEA-NORTH</a>
<span class="fa fa-trash"/>
<span class="type">[queue]</span>
</li></ul></div></div>
I need to click on a button which is having the span class"fa fa-trash" but it is inside li class. And i have list on buttons on the page with li class changing.
I am giving testdata from excel file so i can't use the direct value.
i tried to use this xpath
.//*[#id='routingList']/li[5]/span[1] //testdata1
.//*[#id='routingList']/li[2]/span[1] //testdata2
where li value changes everytime from excel file.
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.visibilityOfElementLocated((By.xpath("//ul[#id='routingList']/li/span[1]")))).click();
List<WebElement> options = driver.findElements(By.xpath("//ul[#id='routingList']/li/span[1]"));
for (WebElement option : options) {
if(testData.equals(option.getText()))
option.click();
Tried above code but it is deleting only one from the list ,where i have passed two more testdata that needs to be deleted.
Need suggestions Please
According to the information you gave me in comments, I think the problem is that you are trying to get a text from an element that doesn't contain text.
Let's say your testData is AS-HTTS-US-LAN-SW. In the HTML you provided and the xpath you mentioned, you are selecting an autoclosing tag <span class="fa fa-trash"/>. Once this tag is selected, you are trying to get the text inside of it, and there is none.
<ul id="routingList" class="ui-sortable">
<li class="ui-menu-item ui-draggable" style="display: list-item;" role="presentation" data-type="srl" data-id="15">
===========================
<a class="ui-corner-all" tabindex="-1">AS-HTTS-US-LAN-SW</a> ----> The text is contained here
<span class="fa fa-trash"/> ---> No text in that tag
===========================
<span class="type">[srl]</span>
</li>
</ul>
So, basically, you have to modify a little bit your xpath from : //ul[#id='routingList']/li/span[1] to : //ul[#id='routingList']/li/a to get the text, and then go back to the parent node to find your button with : ../span[contains(#class, 'fa fa-trash')]
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.visibilityOfElementLocated((By.xpath("//ul[#id='routingList']/li/span[1]")))) // removed the click here because you were clicking on the first element of the list
List<WebElement> options = driver.findElements(By.xpath("//ul[#id='routingList']/li/a"));
for (WebElement option : options) {
if(testData.equals(option.getText()))
option.findElement(By.xpath("../span[contains(#class, 'fa fa-trash')]")).click();
Tell me if it helped
I know you already accepted an answer but there's a more efficient way to do this. You can specify the text you are looking for as part of the XPath. So, you do a single search instead of looping through all the options which can be a performance hit if there are many options. Also, with something like this you are likely to use it more than once so put it in a function.
In this case, the function would take in the string you are looking for and then click the appropriate element.
public void selectRegion(String regionName)
{
driver.findElement(By.xpath("//a[.='" + regionName + "']/following-sibling::span[#class='fa fa-trash']")).click();
}
and you would call it like
selectRegion(testData);
The function looks for an A tag that contains the desired text and then clicks the sibling SPAN with class fa fa-trash.
I am new to Selenium. I am trying to click a sub menu option which has the following code:
<div class="nav-level nav-level-open">
<ul id="menu_0af0fc80e81924533d028c395adb60e8">
<li class="nav-item">
<a class="" href="?s=jobs&ss=jobs&mode=list">
<i class="icn-next" aria-hidden="true"></i>
<span> All Jobs - jobs/ internships/ campus interviews</span>
</a>
</li>
I am trying to click the button here labelled, "All Jobs - jobs/ internships/ campus interviews". Being a menu, there are multiple such links with the same class name. I would prefer locating the element with Css locator or Xpath. Can someone please help me?
Try the below xpath, i am assuming the string "All Jobs - jobs/ internships/ campus interviews" is not repeated in webpage
driver.findElement(By.xpath("//span[contains(text(),' All Jobs - jobs/ internships/ campus interviews')]"));
you can use above strategy in finding target exlements. if the element's display string is not dupicated in webpage
Intall the FirePath Plugin in firefox first and then inspect the button using the firepath. Try with the following code -
driver.findElement(By.xpath("//copy paste the xpath here")).click(); //driver is the reference of WebDriver interface.
driver.FindElement(By.XPath("//ul[#id='menu_0af0fc80e81924533d028c395adb60e8']/li/a")).Click();
Try out this:
WebDriver driver = new FirefoxDriver();
WebeElement element ;
element = driver.findElement(By.xpath("//*[ul[#id='menu_0af0fc80e81924533d028c395adb60e8']/li/a");
driver.click;
Try this :
driver.findElement(By.xpath("//span[contains(text(),' All Jobs - jobs/ internships/ campus interviews')]"));
I want to go to http://ted.com/talks, click the "See All Topics" in the "Topics" dropdown, and then click the a random letter heading, like "C" or "D-E". However, I don't know how to find the element in the modal popup for that specific letter heading.
This is what the letter heading elements look like when I click "Inspect Element":
<li class="topic-select__range">
<a class = "topic-select__range__link" href="#" data-index="0">A-B</a>
</li>
<li class="topic-select__range">
<a class = "topic-select__range__link" href="#" data-index="1">C</a>
</li>
<li class="topic-select__range">
<a class = "topic-select__range__link" href="#" data-index="2">D-E</a>
</li>
... etc.
My program can get all the way to See All Topics just fine but gets Cannot Locate Element Error when I try to click on the letter headers. This is what my code snippet looks like so far:
# Each header is assigned a number. A header is picked at random, and clicked.
# [ERROR: Cannot locate element.]
random_letter = random.randint(0, 8)
topics_window = browser.find_element_by_class_name("select-modal__content topic-select") # error here, when program tries to find modal popup or tags within it
letter_headers = topics_window.find_element_by_class_name("topic-select__range__link")
letter_headers[random_letter].click()
I couldn't find a way to access modal JavaScript content with selenium. I have instead found a way to do so using web API.
Dear Selenium Webdriver Specialists,
I am new to this framework and needed a bit of advice on how to locate / find all the search results from a sales property listing website. Below is a working code
which has successfully found all the properties using Selenium Webdriver but I don't know how to use findElements(by...) to pick up every results returned by
this website:
WebDriver driver = new FirefoxDriver();
driver.get("http://www.domain.com.au/?mode=buy");
// Enter the query string "3000"
WebElement query = driver.findElement(By.xpath(".//*[#id='ctl00_SearchMoreCriteria_Radar_searchToBuy']"));
query.sendKeys("3000");
WebElement searchButton = driver.findElement(By.xpath(".//*[#id='ctl00_SearchMoreCriteria_Radar_Search']"));
searchButton.click();
// Sleep until the div we want is visible or 5 seconds is over
long end = System.currentTimeMillis() + 5000;
Could anyone offer some advice on this query? I also like to include a pausing mechanism before locating / finding all the returned results?
Many thanks,
Jack
Thank you Santoshsarma very much for your response but I am struggling to apply your suggestion onto the following returned web query page output snippet:
<h3>
<a id="ctl00_ctl00_Content_Content_SrchResLst_rptResult_ctl01_EliteListingTemplate_hypAddress" href="/Property/For-Sale/Penthouse/VIC/Melbourne/?adid=2009775619">602/73 Flinders Lane, Melbourne</a></h3>
<dl class="cN-featDetails">
<dt class="proptype">Property type</dt>
<dd id="ctl00_ctl00_Content_Content_SrchResLst_rptResult_ctl01_EliteListingTemplate_ddPropertyType" class="propertytype type-house" title="Property type: House">House</dd>
<dt class="bedrooms">Bedrooms</dt>
<dd id="ctl00_ctl00_Content_Content_SrchResLst_rptResult_ctl01_EliteListingTemplate_ddBedrooms" class="bedrooms" title="Bedrooms">3</dd>
<dt class="bathrooms">Bathrooms</dt>
<dd id="ctl00_ctl00_Content_Content_SrchResLst_rptResult_ctl01_EliteListingTemplate_ddBathrooms" class="bathrooms" title="Bathrooms">4</dd>
<dt class="carspaces">Car spaces</dt>
<dd id="ctl00_ctl00_Content_Content_SrchResLst_rptResult_ctl01_EliteListingTemplate_ddCarSpaces" class="carspaces" title="Car spaces">2</dd>
</dl>
</div>
<div class="main-wrap">
<ul class="photo cfix">
<li>
<a id="ctl00_ctl00_Content_Content_SrchResLst_rptResult_ctl01_EliteListingTemplate_hypMainThumb" tabindex="-1" class="contain" href="/Property/For-Sale/Penthouse/VIC/Melbourne/?adid=2009775619"><img id="ctl00_ctl00_Content_Content_SrchResLst_rptResult_ctl01_EliteListingTemplate_imgMainThumb" title="602/73 Flinders Lane, Melbourne" src="http://images.domain.com.au/img/2012625/18127/2009775619_1_PM.JPG?mod=120925-150000" alt="Main photo of 602/73 Flinders Lane, Melbourne - More Details" style="border-width:0px;" /></a>
</li>
<li class="last">
<a id="ctl00_ctl00_Content_Content_SrchResLst_rptResult_ctl01_EliteListingTemplate_hypSecondThumb" tabindex="-1" class="contain" href="/Property/For-Sale/Penthouse/VIC/Melbourne/?adid=2009775619"><img id="ctl00_ctl00_Content_Content_SrchResLst_rptResult_ctl01_EliteListingTemplate_imgSecondThumb" title="602/73 Flinders Lane, Melbourne" src="http://images.domain.com.au/img/2012625/18127/2009775619_2_PM.JPG?mod=120913-125823" alt="Photo of 602/73 Flinders Lane, Melbourne - More Details" style="border-width:0px;" /></a>
</li>
</ul>
List AllSearchResults=driver.findElements(By.id("ctl00_ctl00_Content_Content_SrchResLst_rptResult_ctl01_EliteListingTemplate_hypAddress"));
There is an index in ctl01 that needs to cycle through to get all search results which would be slow according other similar threats. Is there a better approach to use findElement() on a WebElement to search just its children but I am at a lost on where to locate the root. Are you able to confirm whether this approach would work & perhaps pin point where the root of the list of results from the following URL query result page:
http://www.domain.com.au/Search/buy/Property/Types/Apartment-Unit-Flat/Duplex/House/New-Apartments-Off-the-Plan/New-Home-Designs/New-House-Land/Penthouse/Semi-Detached/Studio/Terrace/Townhouse/Villa/State/VIC/Area/Inner-City/Region/Melbourne-Region/Suburb/Melbourne/?bedrooms=1&bathrooms=1&carspaces=1&from=450000&searchterm=3000&pois=PriSchl|1|2|2000
Your patience for my lack of experience in this area would be very much appreciated.
findElements method will return List of WebElements which has same locator.
> List<WebElement> AllSearchResults=driver.findElements(By.id("value"));
Run the above list in loop to get each individual search result.
for(WebElement eachResult:AllSearchResults)
{
eachResult.click();
}
If this is still unresolved, try the following for locating the h3 link
(new WebDriverWait(driver, 10)).until(ExpectedConditions
.visibilityOfElementLocated(
By.id("ctl00_ctl00_Content_Content_SrchResLst_rptResult_ctl01_EliteListingTemplate_hypAddress")));
This code waits for the given element for 10 seconds and then throws a TimeOutException.
Regards, Christian
May be we can solve it with the help of a custom xpath using ID property.
Code goes like this:
List AllSearchResults=driver.findElements(By.xpath("//*[contains(#id='SrchResLst_rptResult')]"));
or like this:
List AllSearchResults=driver.findElements(By.xpath("//*[contains(#id='ctl00_ctl00_Content_Content_SrchResLst_rptResult_ctl01_EliteListingTemplate_')]"));
Here we used * because some search results are having a combination of tags <a> and <dd>
The xpath searches for all the elements which is containing "SrchResLst_rptResult" string as a value of ID property.