简体   繁体   中英

Select a particular element in a webpage from list using selenium python

I am trying to automate through selenium python to select a particular element from a list of element in a webpage. That is in the first page of the webpage I am selecting one list item from 5 items displayed, later I need to deselect them in the next page of the website where all the five numbers are listed. How can i select that particular element which i have select in the first page of the webpage?

The HTML code of the first page list item looks like this ->

<li class="ng-scope" ng-repeat="line in accountLines">
<label class="list-item inline-group">
<div class="inline-group-addon">
<div class="inline-group-main">
<p class="ng-binding">
</div>

and i have generated xpath as:

.//*[@id='oobe-login']/body/div[1]/main/div/div[1]/ul/li[1]/label/div[2]/p
.//*[@id='oobe-login']/body/div[1]/main/div/div[1]/ul/li[2]/label/div[2]/p
.//*[@id='oobe-login']/body/div[1]/main/div/div[1]/ul/li[3]/label/div[2]/p
.//*[@id='oobe-login']/body/div[1]/main/div/div[1]/ul/li[4]/label/div[2]/p
.//*[@id='oobe-login']/body/div[1]/main/div/div[1]/ul/li[5]/label/div[2]/p

and the HTML code of the next page is which lists all the element is:

li class="ng-scope" ng-repeat="line in allLinesList">
<a class="list-item inline-group reverse highlight" ng-click="expandSettings($index)" ng-class="(settings.selectedLine === $index && settings.expanded) ? 'active' : ''" href="">
</li>

I jus do a text check here which is a hard coding type to select the number that i have selected in the first page.

Can anybody suggest me an solution so that the selenium webdriver clicks each element in the next page and checks if it is registered, if registered, deregister it? Please Help.

I'm not sure which webdriver you are using, but the snippet I'm including here has worked for me with the Firefox, Chrome, and CasperJS drivers.

To click an option in a drop down list, you'll want to use something like the following, using one of your XPATH attributes above:

opt_button = driver.wait.until(EC.element_to_be_clickable((By.XPATH, """.//*[@id='oobe-login']/body/div[1]/main/div/div[1]/ul/li[1]/label/div[2]/p""")))
try:
    opt_button.click() #This is the part that actually selects the option
    print "Clicked the list item!"
except ElementNotVisibleException, s:
    print "Could not click the list item..."
    print "Error: "+str(s)

Selenium offers the ability to wait for an element to be loaded and clickable, so telling your driver to wait for that is usually a good way to prevent errors from slow-loading pages.

As for deselecting the option, you would have to have either a list that offers a NULL selection or a script on the page to clear your selection on page load. I'm not great with JavaScript, but I believe there may be a way to inject a script to deselect a list item. You may want to do a Google search for ways to clear list selections with JS, but I am sure there will be conditions that need to be met in order to do this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM