Given this (the "sleep" method is so you can see what I'm looking at):
from splinter import Browser
import time
#from selenium.webdriver.support.ui import WebDriverWait
#from selenium.webdriver.remote.webdriver import WebDriver
with Browser() as browser:
# Visit URL
url = "https://mdoe.state.mi.us/moecs/PublicCredentialSearch.aspx"
browser.visit(url)
browser.fill('ctl00$ContentPlaceHolder1$txtCredentialNumber', 'IF0000000262422')
# Find and click the 'search' button
button = browser.find_by_name('ctl00$ContentPlaceHolder1$btnSearch')
# Interact with elements
button.first.click()
#implicitly_wait(time_to_wait=5)
time.sleep(30)
I want Splinter/Selenium to click on the link in the right column corresponding to the value "Professional Teaching Certification Renewal" in the column to the left (of the same row).
Here's what I've tried so far (after the code above):
tables=browser.find_by_tag('table') #Get all tables
rows=tables.find_by_tag('tr') #Get all rows in the tables
data=rows.find_by_tag('td') #Get the data(cell)s in the rows
My strategy is to locate the row with the value (data) "Professional Teaching Certification Renewal" and, for that corresponding row, click on the link in the right column. I'm not sure if there is a better strategy, but I'm certainly not married to this one if there is.
I cannot figure out (after reading the docs, of course, unless I missed something) how to examine the data object and determine what it contains. If I can do this, I might be able to figure out the rest.
Thanks in advance!
Answer
To accomplish your goal of clicking the link corresponding to "Professional Teaching Certification Renewal" in the right column of a table, you can refine your strategy. Here's how we can break it down:
Key Steps:
- Locate the table: Extract the rows and columns from the table where this data is displayed.
- Search for the desired text: Find the row that contains the text "Professional Teaching Certification Renewal".
- Click the corresponding link: Once the right row is located, click the link in the right column.
Correct Approach:
- Iterate through the table: First, get the correct table and iterate through its rows.
- Find the cell that matches the desired text: Check the left column (or
td
that contains the relevant text). - Click the link in the right column: If the text in the left column matches, find the corresponding link in the right column and click it.
Updated Code:
from splinter import Browser
import time
with Browser() as browser:
# Visit URL
url = "https://mdoe.state.mi.us/moecs/PublicCredentialSearch.aspx"
browser.visit(url)
browser.fill('ctl00$ContentPlaceHolder1$txtCredentialNumber', 'IF0000000262422')
# Find and click the 'search' button
button = browser.find_by_name('ctl00$ContentPlaceHolder1$btnSearch')
button.first.click()
time.sleep(5) # Allow time for the page to load after search
# Locate the table containing the information
tables = browser.find_by_tag('table') # Get all tables
for table in tables:
rows = table.find_by_tag('tr') # Get all rows in the table
for row in rows:
cells = row.find_by_tag('td') # Get all cells (td) in the row
if len(cells) >= 2: # Ensure the row has at least two cells (left and right columns)
left_column_text = cells[0].text.strip() # Left column text
if left_column_text == "Professional Teaching Certification Renewal":
# Found the row with the matching text, now click the link in the right column
link = cells[1].find_by_tag('a') # Find the link in the right column
if link:
link.first.click() # Click the link
time.sleep(5) # Give time for the new page to load
break # Exit the loop once the link is clicked
Explanation:
- Search for the table: We search for all
table
elements on the page. - Iterate over rows: We loop through each
tr
(table row) within the table. - Check for matching text: For each row, we look at the first column (
td
), and if it contains the text"Professional Teaching Certification Renewal"
, we move to the next step. - Click the link: In the second column (
td
), we look for an anchor (<a>
) tag (which is typically used for links) and click it.
Additional Considerations:
- Error Handling: If there's a chance that the link might not be present in the right column, add error handling to ensure the script doesn't break.
- Delays: I’ve added
time.sleep(5)
after the button click and after clicking the link to give the page time to load, which might need to be adjusted depending on the actual load times. - Element Visibility: You might need to ensure that the elements you're interacting with are visible or interactable. You can add
WebDriverWait
orbrowser.is_element_present_by_*
methods for better control.
Troubleshooting:
If you're not sure about the content of the cells
, you can print the content of each cell to inspect it:
for row in rows:
cells = row.find_by_tag('td')
if len(cells) >= 2:
left_column_text = cells[0].text.strip()
print("Left Column Text:", left_column_text) # Debug print
print("Right Column HTML:", cells[1].html) # Inspect right column content
This will help you understand what is inside each cell and debug if needed.
Let me know if you have any further questions or if things don't work as expected!