0

In selenium, I want to get the element without url.

My code is

driver.find_element(By.XPATH, '//*[@id="pagination"]/ul/li[2][not(/a[@href])]')

But it includes all elements.

What is the correct code?

Thank you very much.

url: https://smartjobs.qld.gov.au then scroll to the bottom and click Search In the new page, scroll to the bottom again, you will see the page number, I want to get the existing page number without "a" attribute page number

  • maybe this will help https://stackoverflow.com/questions/13807499/xpath-select-by-attribute-when-attribute-not-present – Black cat Jul 23 '23 at 04:41

1 Answers1

0

/a means select the element a from the root.

When you want to select li without a a as decsendant try this:

driver.find_element(By.XPATH, '//*[@id="pagination"]/ul/li[2][not(.//a[@href])]')

When you want to select li without a a as child try this:

driver.find_element(By.XPATH, '//*[@id="pagination"]/ul/li[2][not(a[@href])]')

or

driver.find_element(By.XPATH, '//*[@id="pagination"]/ul/li[2][not(./a[@href])]')

where the . current context.

EDIT

This is how your context-html looks like:

<div id="pagination">
  <ul>
    <li class="previous"><input type="submit" name="in_prevBut" value="Previous" class="buttonPrev" border="0"/></li>
    <li>
      &nbsp;
      <a href="#" onmouseover="window.status='Page 1'; return true;">1</a>
      2&nbsp;
      <a href="#" onmouseover="window.status='Page 3'; return true;">3</a>
      &nbsp;
      <a href="#" onmouseover="window.status='Page 4'; return true;">4</a>
      &nbsp;
      <a href="#" onmouseover="window.status='Page 5'; return true;">5</a>
      &nbsp;
      <a href="#" onmouseover="window.status='Page 6'; return true;">6</a>
      &nbsp;
      <a href="#" onmouseover="window.status='Page 7'; return true;">7</a>
      &nbsp;
      <a href="#" onmouseover="window.status='Page 8'; return true;">8</a>
      &nbsp;
      <a href="#" onmouseover="window.status='Page 9'; return true;">9</a>
      &nbsp;
      <a href="#" onmouseover="window.status='Page 10'; return true;">10</a> 
    </li>
  </ul>
</div>

I have navigated to page 2, and we need that number (2).

Since the desired text-string is between a-elements you will need this XPath:

//*[@id="pagination"]/ul/li[2]/text()[string-length(translate(.," &160;","")) > 1]

&#160; = &nbsp;

This will select the text-string with the value of the current page.

Siebe Jongebloed
  • 3,906
  • 2
  • 14
  • 19
  • It returns en error : selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="pagination"]/ul/li[2][not(.//a[@href])]"} – Frankie Chan Jul 23 '23 at 07:56
  • Can you share the url? – Siebe Jongebloed Jul 23 '23 at 09:35
  • url added in the question. – Frankie Chan Jul 23 '23 at 09:42
  • Another error : InvalidSelectorException: Message: invalid selector: The result of the xpath expression "//*[@id="pagination"]/ul/li[2]/text()[string-length(translate(.," &160;","")) > 1]" is: [object Text]. It should be an element. – Frankie Chan Jul 23 '23 at 11:48
  • That s something you should take care of. Take a good look at my answer and how the html looks like. If you need the li with alle of it's content, you will have enough of `//*[@id="pagination"]/ul/li[2]` – Siebe Jongebloed Jul 23 '23 at 11:56
  • I still can't figure out the problem. – Frankie Chan Jul 23 '23 at 12:52
  • Okay, since selenium does not offer to get a text-node using XPath, you have to do this by getting all the text form this li and than by using your scripting-language to get the actual page-number. See https://stackoverflow.com/a/8506502/3710053 – Siebe Jongebloed Jul 23 '23 at 16:27