简体   繁体   中英

Looping through to find element by xpath and then click on each element found

Web page I am trying to automate contains a list of 100+ links (teachers' names), clicking on which loads the list of students registered under that teacher. My task is to click on each link to make sure students information loads (on same page), click browser back button, click on 2nd teacher name and so on till end of the last link (teacher's name).

from selenium import webdriver
import time
driver=webdriver.Ie()
driver.get('I load my main page')
tablecontent=driver.find_elements_by_xpath("//table/tbody/tr/td/a") #see html code  provided below
tablelen=len(tablecontent) #find number of links on main page.
#loop through each link
for i in range(2,tablelen):
  driver.find_element_by_xpath("//table/tbody/tr[i]/td/a").click() #plug in i value to tr
  time.sleep(2)
  driver.back()

Above code generates below error

Traceback (most recent call last):
  File "C:/Python27/syn_xpath.py", line 14, in <module>
    driver.find_element_by_xpath("//table/tbody/tr[i]/td/a").click()
NoSuchElementException: Message: Unable to find element with xpath == //table/tbody/tr[i]/td/a

If I replace tr[i] by tr[2] (or any integer value within the brackets), it recognizes the element and clicks on it. Eg, driver.find_element_by_xpath("//table/tbody/tr[2]/td/a").click() works just fine. Any idea why passing integer value to tr via i (in a for loop) does not work?

HTML code:

<table>
<tr><td nowrap><b>Teacher</b></td><td nowrap><b>School</b></td></tr>
<tr><td><a href="/db/status.php?teacherid=1234">Teacher1</a></td><td>XYZ High School</td></tr>
<tr><td><a href="/db/status.php?teacherid=5678">Teacher2</a></td><td>ABC School</td></tr>
<tr><td><a href="/db/status.php?teacherid=1111">Teacher3</a></td><td>International School</td></tr>

You need to format the xpath string.

By doing

"//table/tbody/tr[i]/td/a"

it is literally putting tr[i] into the xpath, not tr[1] etc.,

Instead of

driver.find_element_by_xpath("//table/tbody/tr[i]/td/a").click()

do

x_path = "//table/tbody/tr[{0}]/td/a".format(i)
driver.find_element_by_xpath(x_path).click()

This replaces the {1} in the string with the value you provide in the format function, so in this case it will replace {1} with 1 , 2 etc. and will make your xpath selector work as expected.

Example:

search_result1 = sel.find_element_by_xpath("//a[not((//div[contains(@class,'s')]//div[contains(@class,'kv')]//cite)[1])]|((//div[contains(@class,'s')]//div[contains(@class,'kv')]//cite)[1])").text

search_result2 = sel.find_element_by_xpath("//a[not((//div[contains(@class,'s')]//div[contains(@class,'kv')]//cite)[2])]|((//div[contains(@class,'s')]//div[contains(@class,'kv')]//cite)[2])").text


search_results=[]
for i in range(1,11) #I am assuming 10 results in a page so you can set your own range
    result=sel.find_element_by_xpath("//a[not((//div[contains(@class,'s')]//div[contains(@class,'kv')]//cite)[%s])]|((//div[contains(@class,'s')]//div[contains(@class,'kv')]//cite)[%s])"%(i,i)).text
    search_results.append(result)

So I altered your code

driver.find_element_by_xpath("//table/tbody/tr[%s]/td/a")%(i).click() 

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