'Click a link if the previous value in a column contains a value selenium python
I have a table with a hierarchy of item> sub-item > sub2-item sub-2 item name can be the same as item. Plug-in Names for item > sub-item > sub2-item will be unique. I want to click on the second oranges so item 1 > sub-slot 1 > plugin 01
How do I do this? Here is a photo of the page. https://drive.google.com/file/d/1LQD8VYzCrqucStvNd0gFLRrMoiei2NVz/view?usp=sharing
Here is some sample code of the table of what id's/names we can work with.
`
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=8" />
<tbody><tr jx:obj="9051445415013079394" jx:row="0">
<td class="Default itemCheckboxAlign"><input type="checkbox" class="checkbox" name="items" value="9051445415013079394" onclick="selectRow(this, event);"/></td>
<td class="Default"><a href="/ncobject.jsp?id=9051445415013079394" class="refImage" style="background-image:url(/img/ico/types/ico_slot.gif);">01</a></td>
<td class="Default">Slot</td>
<td class="Default"><img src="/img/ico/ico_card_active.gif" alt=""/> Card: <a href="/ncobject.jsp?id=9091944643013188428">APPLES</a></td>
<td class="Default"><div style="margin:2px 0;"><span style="font-size:12px;border:1px solid black;"><span style="border-left:6ex solid #CBCDDE;color:black;">‌</span></span> Installed</div></td>
<td class="Default">1</td>
<td class="Default"></td>
<td class="Default">05/14/2009 05:36:55</td>
<td class="Default Expanding"></td>
</tr>
<tr jx:obj="9091945000013664053" jx:row="1">
<td class="Default itemCheckboxAlign"><input type="checkbox" class="checkbox" name="items" value="9091945000013664053" onclick="selectRow(this, event);"/></td>
<td class="Default"><div style="margin-left:16px;"><a href="/ncobject.jsp?id=9091945000013664053" class="refImage" style="background-image:url(/img/ico/ico_slot.gif);">(F) MDA1</a></div></td>
<td class="Default">Slot</td>
<td class="Default"><div style="margin-left:16px;"><img src="/img/ico/ico_card_active.gif" alt=""/> Card: <a href="/ncobject.jsp?id=9091944736013229106">KIWI</a></div></td>
<td class="Default"><div style="margin:2px 0;"><span style="font-size:12px;border:1px solid black;"><span style="border-left:6ex solid #CBCDDE;color:black;">‌</span></span> Installed</div></td>
<td class="Default"></td>
<td class="Default"></td>
<td class="Default"></td>
<td class="Default Expanding"></td>
</tr>
<tr jx:obj="9091945219013235290" jx:row="2">
<td class="Default itemCheckboxAlign"><input type="checkbox" class="checkbox" name="items" value="9091945219013235290" onclick="selectRow(this, event);"/></td>
<td class="Default"><div style="margin-left:32px;"><a href="/ncobject.jsp?id=9091945219013235290" class="refImage" style="background-image:url(/img/ico/ico_slot.gif);">(F) PORT 01</a></div></td>
<td class="Default">Slot</td>
<td class="Default"><div style="margin-left:32px;"><img src="/img/ico/ico_card_active.gif" alt=""/> Card: <a href="/ncobject.jsp?id=9091944744013328176">ORANGES</a></div></td>
<td class="Default"><div style="margin:2px 0;"><span style="font-size:12px;border:1px solid black;"><span style="border-left:6ex solid #CBCDDE;color:black;">‌</span></span> Installed</div></td>
<td class="Default"></td>
<td class="Default"></td>
<td class="Default"></td>
<td class="Default Expanding"></td>
</tr>
<tr jx:obj="9091944994013589646" jx:row="3">
<td class="Default itemCheckboxAlign"><input type="checkbox" class="checkbox" name="items" value="9091944994013589646" onclick="selectRow(this, event);"/></td>
<td class="Default"><div style="margin-left:32px;"><a href="/ncobject.jsp?id=9091944994013589646" class="refImage" style="background-image:url(/img/ico/ico_slot.gif);">(F) PORT 02</a></div></td>
<td class="Default">Slot</td>
<td class="Default"><div style="margin-left:32px;"><img src="/img/ico/ico_card_active.gif" alt=""/> Card: <a href="/ncobject.jsp?id=9132625664313135067">ORANGES</a></div></td>
<td class="Default"><div style="margin:2px 0;"><span style="font-size:12px;border:1px solid black;"><span style="border-left:6ex solid #CBCDDE;color:black;">‌</span></span> Installed</div></td>
<td class="Default"></td>
<td class="Default"></td>
<td class="Default"></td>
<td class="Default Expanding"></td>
</tr>
<tr jx:obj="9091945196013977855" jx:row="4">
<td class="Default itemCheckboxAlign"><input type="checkbox" class="checkbox" name="items" value="9091945196013977855" onclick="selectRow(this, event);"/></td>
<td class="Default"><div style="margin-left:32px;"><a href="/ncobject.jsp?id=9091945196013977855" class="refImage" style="background-image:url(/img/ico/ico_slot.gif);">(F) PORT 03</a></div></td>
</script>
</body>
</html>
` ''' This is what I've tried so far. I'm able to print "found card" so it locates the web element but i'm having trouble clicking on it or finding any attributes from it.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
import secrets
import time
driver.get("INTERNAL WEBSITE")
elem = WebDriverWait(driver, 30).until(
EC.presence_of_element_located((By.ID, "id__v7091959061013574636_0")) # This is a dummy element
)
table_id = driver.find_element(By.ID, 's_t')
rows = table_id.find_elements(By.TAG_NAME, "tr")
# cycle through the list of slots
for row in rows:
#print(row.text)
# print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
# # Get the columns
col_name = row.find_elements(By.TAG_NAME, "td")[1] # This is the Name Column
if col_name.text == "02":
col_card_name = row.find_elements(By.TAG_NAME, "td")[3] # This is the Cards Column
print(col_card_name.text)
if col_card_name.text == "ORANGES":
print("Found card.")
card = col_card_name.find_elements(By.XPATH,".//p[text() = 'ORANGES']").click
'''
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
