Étant donné une structure XML comme celle-ci:
<table> <thead> <tr> <th><div data-col="0">First Name</div></th> <th><div data-col="1">Last Name</div></th> <th><div data-col="2">Age</div></th> <th><div data-col="3">Id</div></th> <th><div data-col="4">City</div></th> </tr> </thead> <tbody> <tr data-row="0"> <td data-col="0"><div>Ivan</div></td> <td data-col="1"><div>Ivanov</div></td> <td data-col="2"><div>35</div></td> <td data-col="3"><div>EFF-12218</div></td> <td data-col="4"><div>London</div></td> </tr> </tbody> </table>
data-col
(où text = 'id'
), pour le
premier élément? 'EFF-12218'
) en utilisant la valeur trouvée de data-col
? 4 Réponses :
String colNumber = driver.findElement (By.xpath ("// div [text () = 'Id']")). getAttribute ("data-col");
p >
driver.findElement (By.xpath ("// td [@ data-col = '" + colNumber + "']")). getText ();
Pour récupérer la valeur de data-col où text est id
, vous pouvez utiliser l'une des méthodes suivantes Stratégies de localisation :
Utilisation de XPATH
:
data-col = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//table//th//div[contains(., 'Id')]"))).get_attribute("data-col") print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//table//tbody/tr//td[@data-col='"+ data-col +"']/div"))).get_attribute("innerHTML"))
Idéalement, vous devez induire WebDriverWait pour le visibilité_of_element_located ()
et vous pouvez utiliser la solution suivante:
Utilisation de XPATH
:
data-col = driver.find_element_by_xpath("//table//th//div[text()='Id']").get_attribute("data-col") print(driver.find_element_by_xpath("//table//tbody/tr//td[@data-col='"+ data-col +"']/div").get_attribute("innerHTML"))
Remarque : vous devez ajouter les importations suivantes:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
Pour récupérer la valeur EFF-12218 par rapport à data-col = "3"
, vous pouvez utilisez la solution suivante:
Utilisation de XPATH
:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//table/thead//th/div[contains(., 'Id')]"))).get_attribute("data-col"))
Idéalement, vous devez induire WebDriverWait pour le visibilité_of_element_located ()
et vous pouvez utiliser la solution suivante:
Utilisation de XPATH
:
print(driver.find_element_by_xpath("//table/thead//th/div[text()='Id']").get_attribute("data-col"))
Réponse très complète.
Pour utiliser la valeur de l'attribut Son résultat est EFF-12218 comme prévu. qui fait partie de la deuxième question. data-col
comme index des enfants de l'élément , utilisez cette expression XPath-1.0
//thead/tr/th[div/text()='Id']/div/@data-col
La réponse à votre première question est d'ailleurs la suivante //tbody/tr/td[@data-col=//thead/tr/th[div/text()='Id']/div/@data-col]
Reportez-vous à la réponse ci-dessous, cela vous aidera à obtenir le texte de n'importe quelle cellule. vous devez simplement changer la valeur de la ligne et de la colonne dans xpath:
WebDriver driver = new ChromeDriver(); // to Get Text of ID( you can store into array/arraylist // By Changing the row column value in xpath //tbody/tr[row]/td[column]) you can get //data of any cell of table List<WebElement> id_elements = driver.findElements(By.xpath("//tbody/tr/td[4]")); int noOfRow= id_elements.size(); for(int i=0; i<=noOfRow;i++) { String id = driver.findElement(By.xpath("//tbody/tr["+i+"]/td[4]")).getText(); System.out.println(id); }
J'espère que cela vous donne une idée!