Je dois cliquer sur l'élément avec le texte comme C1111-8PLTELAWZ dans le TR. Quand j'ai utilisé le XPath "xpath (" // table [@ id = 'tbl'] / tbody / tr [2] / td [2] "). Click ()" , `
face à l'erreur ci-dessous:
<span id="UpdatePanel1">
<table id="tbl" class="device_settings" x-ms-format-detection="none" style="width:100%;border-collapse:collapse;" cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr>
<th class="cell_header" style="width:20px;"><input type="checkbox" onclick="CheckAllRows(this);" id="IsCheckAllRows"></th>
<th class="cell_header" onclick="SetOrder( 'manufacturer_name')" style="CURSOR:pointer;"><span style="text-decoration:underline;">Manufacturer</span></th>
<th class="cell_header" onclick="SetOrder( 'product_class_model')" style="CURSOR:pointer;"><span style="text-decoration:underline;">Model name</span></th>
<th class="cell_header" onclick="SetOrder( 'device_serial')" style="CURSOR:pointer;"><span style="text-decoration:underline;">Serial</span></th>
<th class="cell_header" onclick="SetOrder( 'device_created')" style="CURSOR:pointer;"><span style="text-decoration:underline;">Created</span></th>
<th class="cell_header" onclick="SetOrder( 'device_updated')" style="CURSOR:pointer;"><span style="text-decoration:underline;">Updated</span><img src="../Images/down.gif"></th>
<th class="cell_header_a" onclick="SetOrder( 'device_status')" style="CURSOR:pointer;"><span style="text-decoration:underline;">Status</span></th>
</tr>
<tr isactive="Ok" rowid="9" onmouseout="RowOut(this, event)" onmouseover="RowOver(this, event);" onclick="RowClick('9',event);" style="cursor:pointer;">
<td class="cell_inactive_item" onclick="CheckRow(this, event);" style="width:20px;cursor:default;" align="center"><input type="checkbox" onclick="CheckRow(this, event);"></td>
<td class="cell_inactive_item" align="center">Cisco Systems Inc</td>
<td class="cell_inactive_item" align="center">C1117-4PLTEEA</td>
<td class="cell_inactive_item" align="center">FGL212891Z6</td>
<td class="cell_inactive_item" align="center"><span style="white-space:nowrap;">2/15/2019 11:37:14</span></td>
<td class="cell_inactive_item" align="center"><span style="white-space:nowrap;">2/15/2019 14:12:50</span></td>
<td class="cell_inactive_item_a" align="center">0</td>
</tr>
</table>
</span>
Le code HTML:
Traceback (most recent call last):
File "acs.py", line 17, in <module>
qwe = browser.find_element_by_xpath("//table[@id='tbl']/tbody/tr[2]/td[2]").click()
File "/users/hemgr/selenium/webdriver/remote/webdriver.py", line 394, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "/users/hemgr/selenium/webdriver/remote/webdriver.py", line 978, in find_element
'value': value})['value']
File "/users/hemgr/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/users/hemgr/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: //table[@id='tbl']/tbody/tr[2]/td[2]
3 Réponses :
1) Votre XPath n'est pas correct si vous voulez la valeur "C1111-8PLTELAWZ" de la table. Il devrait être:
from selenium.webdriver.support import ui from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By ui.WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//span[@id='UpdatePanel1']//table[@id='tbl']/tbody/tr[2]/td[3]"))).click()
2) Il vaut mieux utiliser l'attente explicite pour être cliquable sur l'élément avant d'interagir avec lui (pour éviter les problèmes):
//span[@id='UpdatePanel1']//table[@id='tbl']/tbody/tr[2]/td[3]
J'ai ajouté dans la section Code, à partir de span id.
Vous utilisez un xpath absolu, ce qui n'est pas une bonne pratique à utiliser:
Vous pouvez essayer:
//td[@class='cell_inactive_item' and contains(text(),'C1111-8PLTELAWZ')]
Vous essayez peut-être d'appeler click () sur l'élément qui se trouve juste en dessous du avec le texte comme Nom du modèle . Pour y parvenir, vous pouvez utiliser la stratégie de localisation : < pré> XXX