Python Selenium Loop Through Table Elements

谁说胖子不能爱 提交于 2021-02-11 15:37:26

问题


I know there are similar questions out there. I have read many (if not all) of them and am still lost.

I'm writing a script to automate some data retrival/entry and need to iterate over an unknown number of entries in a table on a web page. I created a sample you can see here:

So far my script logs into this ERP system, navigates to the page in the screenshot, and then waits for the StandardGrid to load with this:

WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "//table[@class='StandardGrid']")))

The StandardGrid is where the links are housed on the web page that I want to iterate over. It's here's where I get lost.

I've tried several different find_elements_by_XYZ but can't figure out for the life of me how to get it to work. I've tried using ChroPath with Inspect in the browser to identify every way I could think of to get this to work.

The best I could come up with is in this case, the main table that contains the data I want to iterate over has an XPath of "//table[@class='StandardGrid']"

Therefore, I tried the following:

my_list = driver.find_elements_by_xpath("//a[@class='StandardGrid']")
for item in my_list:
    print(item)

But nothing prints out.

The table header of the column I want to iterate over and click on all the links of has the tag of <th xpath="1">Operation</th>

In this screenshot, the first URL has a tag of

'<a href="../Modules/Engineering/ProcessRoutings/PartOperationForm.aspx?Do=Update&amp;Part_Operation_Key=8355805&amp;Part_Key=2920988&amp;Part_No=WP112789+Rev%2E%2D&amp;Revision=-&amp;Image=&amp;Operation=Polish" onmouseover="status='Go To Detail Form'; return true;" onmouseout="status='';" style="" xpath="1">
Polish
</a>'

With the data I'm using there are hundreds of possible links like the one above, so a proper dynamic solution is needed.

Any help is greatly appreciated, I'm totally stuck. Thank you!


回答1:


Easiest way to get links directly:

operations = wait.until(
    EC.visibility_of_all_elements_located((By.CSS_SELECTOR, 'table.StandardGrid a[onmouseover*="Go To Detail Form"]')))

for operations in operations:
    print(operations.text, operations.get_attribute['href'])


来源:https://stackoverflow.com/questions/59901360/python-selenium-loop-through-table-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!