Reputation: 340
I have following dynamic webtable
(checkbox) ID_No country_code Date Time FileName
1A 5J 10/04/2019 12:05:45 filename_12:05
1A 5J 10/04/2019 13:05:45 filename2
3A 8J 10/03/2019 14:05:45 filename2
4A 9J 10/04/2019 14:08:45 filename1
On the left hand side of the id_no there is a checkbox.
I have following dataframe
ID_No country_code Date Time FileName
1A 5J 10/04/2019
1A 5J 10/04/2019
I only want to check the rows in the dynamic webtable correspoing to the matching country_code,ID_No and Date in the dataframe,and populate the Time and filename values in the dataframe
I am writing below code in a loop selecting 1 checkbox at a time
for index,row in df.iterrows():
id_no=row['ID_No']
country_code=row['country_code']
date=row['Date']
driver = webdriver.Chrome()
driver.get(website_URL)
driver.find_element_by_xpath("//tr[td[contains(text(),date)] and
td[contains(text(),country_code)] and td[contains(text(),id_no)] ]//input").click()
in this case country_code='5J' and ID_No='1A' and Date='10/04/2019' when I use this xpath in the chrome console:
$x("//tr[td[contains(text(),'10/04/2019')] and
td[contains(text(),'5J')] and td[contains(text(),'1A')] ]")[0]
This contains 1st row that satisfies above condition.I get following HTML for the rows of this webtable
<tr class="class1">
<td width="2%"> </td> <!–– checkbox ––>
<td width="2%"> 1A </td>
<td width="2%"> 5J </td>
<td width="2%"> 10/04/2019 </td>
<td width="2%"> 12:05:45 </td>
<td width="2%"> filename_12:05 </td>
</tr>
How can I extract corresponding filename and time from the dynamic webtable corresponding to the checkboxes I select?
in the above case,in the first iteration,extracted_filename='filename_12:05' and time='12:05:45'
second_iteration,extracted_filename='filename2' and time='13:05:45
Upvotes: 0
Views: 294
Reputation: 33384
To handle dynamic element Induce WebDriverWait
and following xpath.
print(WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//tr[td[contains(text(),'" + date + "')] and td[contains(text(),'" + country_code +"')] and td[contains(text(),'" + id_no + "')] ]//input/following::td[4]"))).text)
print(WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//tr[td[contains(text(),'" + date + "')] and td[contains(text(),'" + country_code +"')] and td[contains(text(),'" + id_no + "')] ]//input/following::td[5]"))).text)
To execute above code you need to import followings.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Upvotes: 1
Reputation: 1574
Can you try these xpaths and let me know how it goes.
This will give you the time of matching row
time= driver.find_element_by_xpath("//tr[td[contains(text(),date)] and td[contains(text(),country_code)] and td[contains(text(),id_no)] ]//input/../following-sibling::td[4]").text
This will give you the file name of matching row
filename=driver.find_element_by_xpath("//tr[td[contains(text(),date)] and td[contains(text(),country_code)] and td[contains(text(),id_no)]]//input/../following-sibling::td[5]").text
With your code:
for index,row in df.iterrows():
id_no=row['ID_No']
country_code=row['country_code']
date=row['Date']
driver = webdriver.Chrome()
driver.get(website_URL)
driver.find_element_by_xpath("//tr[td[contains(text(),date)] and td[contains(text(),country_code)] and td[contains(text(),id_no)] ]//input").click()
time= driver.find_element_by_xpath("//tr[td[contains(text(),date)] and td[contains(text(),country_code)] and td[contains(text(),id_no)] ]//input/../following-sibling::td[4]").text
filename=driver.find_element_by_xpath("//tr[td[contains(text(),date)] and td[contains(text(),country_code)] and td[contains(text(),id_no)]]//input/../following-sibling::td[5]").text
Upvotes: 1