HTML:
<div id="testModal" role="dialog" aria-labelledby="testModal_title" aria-hidden="false" aria-live="polite" style="width: auto; height: auto; left: 495px; top: 362px; z-index: 1002; display: block;"><div ><span id="testModal_title" ></span></div><div style="height: auto;">
<form id="testForm" name="testForm" method="post" action="/pages/juht/test/test.jsf" enctype="application/x-www-form-urlencoded">
<input type="hidden" name="testForm" value="testForm">
<ul>
<li><a id="testForm:j_idt607:8:j_idt609" href="#" onclick="PrimeFaces.addSubmitParam('testForm',{'testForm:j_idt607:8:j_idt609':'testForm:j_idt607:8:j_idt609'}).submit('testForm');return false;PrimeFaces.onPost();">
<strong>10.10.2022</strong>
19:00
<strong>Place »</strong></a>
</li>
</li>
<li><a id="testtestForm:j_idt607:10:j_idt609" href="#" onclick="PrimeFaces.addSubmitParam('testForm',{'testForm:j_idt607:10:j_idt609':'testForm:j_idt607:10:j_idt609'}).submit('testForm');return false;PrimeFaces.onPost();">
<strong>11.10.2022</strong>
11:00
<strong>Place2 »</strong></a>
</ul>
<input type="hidden" name="javax.faces.ViewState" value="-4629203658604947866:263715935893442864" autocomplete="off"></form></div></div>
How can I print out the element id by just using strong text here?
CodePudding user response:
You want the parent element's ID of that <strong>
element? Then try:
element = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//strong[text()='10.10.2022']::a")))
element_id = element.get_attribute('id')
You will need to also import the following:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
CodePudding user response:
To print the value of the id attribute as the elements are dynamic you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following locator strategies:
To print
testForm:j_idt607:8:j_idt609
:Using XPATH and the text 19:00:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[contains(., '19:00')]"))).get_attribute("id"))
Using XPATH and the text 10.10.2022:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[.//strong[contains(., '10.10.2022')]]"))).get_attribute("id"))
Using XPATH and the text Place »:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[.//strong[contains(., 'Place »')]]"))).get_attribute("id"))
To print
testtestForm:j_idt607:10:j_idt609
:Using XPATH and the text 11:00:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[contains(., '11:00')]"))).get_attribute("id"))
Using XPATH and the text 11.10.2022:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[.//strong[contains(., '11.10.2022')]]"))).get_attribute("id"))
Using XPATH and the text Place2 »:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[.//strong[contains(., 'Place »')]]"))).get_attribute("id"))
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC