HTML:
<div id="testModal" class="ui-dialog ui-widget ui-widget-content ui-corner-all ui-shadow ui-hidden-container ui-dialog-exam ui-draggable" 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 class="ui-dialog-titlebar ui-widget-header ui-helper-clearfix ui-corner-top ui-draggable-handle"><span id="testModal_title" class="ui-dialog-title"></span></div><div class="ui-dialog-content ui-widget-content" 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="#" class="ui-commandlink ui-widget" 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="#" class="ui-commandlink ui-widget" 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?
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