Python селен для l oop над строками в таблице - PullRequest
0 голосов
/ 19 марта 2020

Я пытаюсь l oop по каждой строке таблицы, используя Python Selenium, и щелкаю первую ячейку строки. Когда я запускаю свой код, он зацикливается дважды, как и ожидалось, и сохраняет каждый документ в нужном месте назначения. Проблема в том, что он выбирает первый ряд дважды, поэтому вместо каждого уникального изображения я получаю две копии одного и того же изображения. Вот моя попытка:

#Find the table I want
table_id = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "/html/body/ted-root/ted-search/ted-result/mat-table")))

#Find all rows in the table
rows = (table_id.find_elements_by_xpath("//mat-row"))

#print elements of rows found
print(rows)

#Use for starting points for loop
i =  0
j = 0

#Instead of looping over the element names, I choose to loop over the index of row numbers because the elements change when I nav away from the page 
row_count = len(rows)

for row_index in range(0, row_count):
    #Refind the table I want
    table_id = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "/html/body/ted-root/ted-search/ted-result/mat-table")))

    #Find all rows in the table                       I hope this selects the next row each time it loops
    row = table_id.find_elements_by_xpath("//mat-row")[row_index]
    print(row)

    #Here I find the first cell element in the row to click so that I nav to the document page
    cell = row.find_element_by_xpath('//mat-cell')
    print(cell)

    cell.click()

    #This finds the path of the document on the page and clicks it to view the image
    document = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CLASS_NAME,'doc-img')))

    document.click()
    time.sleep(4)

    #This screen prints the image and saves it to my documents
    driver.execute_script('window.print();')

    file_path = buf.value + "\\" + CardNumber + '_' + str(i) + ".pdf"

    os.rename(buf.value + "\\Tax Exempt Document Management.pdf",file_path)

    source = str(file_path)

    destination = 'C:/Users/A0C08AX/Documents/IBM_downloads'

    shutil.move(source, destination)

    #This navs back two windows to the page that contains the table so that next loop can find the new element name of the table and start over
    driver.execute_script("window.history.go(-2)")
    print("Document " + CardNumber + str(i) + " Printed")
    i = i+1
    time.sleep(6)

Мой вывод:

[<selenium.webdriver.remote.webelement.WebElement (session="40575b8e6b90b9e897918a02bd2c438f", element="4cb7148e-a062-487f-8013-e19d97cc9aaa")>, <selenium.webdriver.remote.webelement.WebElement (session="40575b8e6b90b9e897918a02bd2c438f", element="02682aef-f3f7-421b-a9bc-f77793d504a0")>]
<selenium.webdriver.remote.webelement.WebElement (session="40575b8e6b90b9e897918a02bd2c438f", element="4cb7148e-a062-487f-8013-e19d97cc9aaa")>
<selenium.webdriver.remote.webelement.WebElement (session="40575b8e6b90b9e897918a02bd2c438f", element="6a93bc78-72e4-4daf-9409-8d7e6974a531")>
'C:/Users/A0C08AX/Documents/IBM_downloads\\15848187_0.pdf'
Document 158481870 Printed
<selenium.webdriver.remote.webelement.WebElement (session="40575b8e6b90b9e897918a02bd2c438f", element="1bef5070-fd06-4338-bc17-17bd55975688")>
<selenium.webdriver.remote.webelement.WebElement (session="40575b8e6b90b9e897918a02bd2c438f", element="6fc2abf7-cb4e-4741-b20d-35adf09fe144")>
'C:/Users/A0C08AX/Documents/IBM_downloads\\15848187_1.pdf'
Document 158481871 Printed

Вы можете видеть, что элементы строк меняются, когда я возвращаюсь на две страницы назад и перепечатываю элементы строки. По какой-то причине я не понимаю, почему row_index в моем коде не выбирает второй элемент строки на второй итерации l oop.

...