Working with Python and Selenium Chrome Driver

Viewed 51

I was hoping for maybe some clarification or suggestions on how to get selenium working with a href I have tried selecting it by pretty much every element possible.

My end goal would be to loop through each "Case Number" and click on it and once finished add each one to a list, then if it is not in the list click on it until finished.

<td class="x-grid3-col x-grid3-cell x-grid3-td-CASES_CASE_NUMBER " style="width:172px;" tabindex="0"><div class="x-grid3-cell-inner x-grid3-col-CASES_CASE_NUMBER" id="5005a00001rezYE_CASES_CASE_NUMBER"><a href="/5005a00001rezYE">0000000</a></div></td>

<div class="x-grid3-cell-inner x-grid3-col-CASES_CASE_NUMBER" id="5005a00001rezYE_CASES_CASE_NUMBER"><a href="/5005a00001rezYE">0000000</a></div>

<a href="/5005a00001rezYE">0000000</a>

clicky = driver.find_element_by_css_selector("//div[@id='5005a00001rezYE_CASES_CASE_NUMBER']").click()

I have also tried with this element(which would make it easier for what I am trying to do)


<a href="/5005a00001uLh3i/e?retURL=%2F500%3Ffcf%3D00B0b000007JS3z%26rolodexIndex%3D-1%26page%3D1"><span>Edit</span></a>
<span>Edit</span>

clicky = driver.find_element_by_xpath("span[contains(text(),'Edit')]").click()
clicky = driver.find_element_by_css_selector("span[Edit]").click()

1 Answers

I'm not sure this locator is correct and unique, but you can try this:

clicky = driver.find_element_by_xpath("//span[contains(text(),'Edit')]").click()

Possibly you will have to add some delay / wait before accessing this element.
The simplest way is to add a delay

time.sleep(5)
clicky = driver.find_element_by_xpath("//span[contains(text(),'Edit')]").click()

While recommended approach is to add an explicit wait, like this:

WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//span[contains(text(),'Edit')]"))).click()

For this you will need to add these imports:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Related