[FIXED] So erhalten Sie Span-Tag-Text mit Selen für Python

Ausgabe

Mit dem folgenden HTML-Code möchte ich den Wert des dritten span-Tags innerhalb des div-Tags abrufen. Wenn ich beispielsweise auf die ID psr2 geklickt habe, möchte ich den Wert „aaaa, bbbb“ abrufen.

Ich habe diesen Code geschrieben:

    n = randint(0,9)
    userPos = 'psr'+str(n)
    WebDriverWait(browser, maxTimeout).until(EC.element_to_be_clickable((By.ID, str(userPos)))).click()
    userName = WebDriverWait(browser, maxTimeout).until(EC.visibility_of_element_located((By.ID, "countLabel")[n])).text
    print(userName)

Der HTML-Code:

<div id="psr2" uid="19202" class="treeNode">
  <nobr>
    <a class="focusableNode" href="#r2" onclick="return false;" tabindex="0" onkeydown="ps.onKeyDown(this);">
      <span role="presentation">
        <span id="countLabel" class="hidden-label">Elément de liste 3 sur {5}</span>
        <span>aaaa, bbbb</span>
        <label name="SelectInfo" class="hidden-label"></label>
      </span>
    </a>
  </nobr>
</div>


<div id="psr3" uid="6653" class="treeNode">
  <nobr>
    <a class="focusableNode" href="#r3" onclick="return false;" tabindex="0" onkeydown="ps.onKeyDown(this);">
      <span role="presentation">
        <span id="countLabel" class="hidden-label">Elément de liste 4 sur {5}</span>
        <span>xxxx, yyyy</span>
        <label name="SelectInfo" class="hidden-label"></label>
      </span>
    </a>
  </nobr>
</div>

Könnten Sie mir bitte helfen, den Benutzernamen zu finden?

Mit freundlichen Grüßen

Lösung

Verwendenfollowing-sibling xpath

n = randint(0,9)
userPos = 'psr'+str(n)
WebDriverWait(browser, maxTimeout).until(EC.element_to_be_clickable((By.ID, str(userPos)))).click()
userName = WebDriverWait(browser, maxTimeout).until(EC.visibility_of_element_located((By.XAPTH, "//span[@id='countLabel']/following-sibling::span"))).text
print(userName)

oderfollowing

n = randint(0,9)
userPos = 'psr'+str(n)
WebDriverWait(browser, maxTimeout).until(EC.element_to_be_clickable((By.ID, str(userPos)))).click()
userName = WebDriverWait(browser, maxTimeout).until(EC.visibility_of_element_located((By.XAPTH, "//span[@id='countLabel']/following::span[1]"))).text
print(userName)

Aktualisieren.

n = randint(0,9)
userPos = 'psr'+str(n)
WebDriverWait(browser, maxTimeout).until(EC.element_to_be_clickable((By.ID, str(userPos)))).click()
xpath="//div[@id='{}']//span[@id='countLabel']/following-sibling::span".format(userPos)
userName = WebDriverWait(browser, maxTimeout).until(EC.visibility_of_element_located((By.XAPTH,xpath))).text
print(userName)
userName = WebDriverWait(browser, maxTimeout).until(EC.visibility_of_element_located((By.XAPTH,xpath))).get_attribute(textContent)
print(userName)


Beantwortet von –
KunduK


Antwort geprüft von –
Willingham (FixError Volunteer)

0 Shares:
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like