简体   繁体   English

如何使用 Selenium Python 通过强文本获取元素 id

[英]How to get the element id by strong text using Selenium Python

HTML: 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?如何仅在此处使用强文本打印出元素 ID?

You want the parent element's ID of that <strong> element?您想要该<strong>元素的父元素 ID? 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

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 :要打印id属性的值,因为元素是动态的,您需要为visibility_of_element_located()引入WebDriverWait ,您可以使用以下任一定位器策略

  • To print testForm:j_idt607:8:j_idt609 :打印testForm:j_idt607:8:j_idt609

    • Using XPATH and the text 19:00 :使用XPATH和文本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 :使用XPATH和文本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 » :使用XPATH和文本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 :要打印testtestForm:j_idt607:10:j_idt609

    • Using XPATH and the text 11:00 :使用XPATH和文本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 :使用XPATH和文本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 » :使用XPATH和文本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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM