0

I need selenium to select a button with the html code of:

<a href="stuMain.php?datasetID=31561">583</a>

There are over a thousand buttons and each button has two different numbers in the datasetID and the number after that. I do not need to select a certain button, I just need one with the second number present and the datasetID present as well.

I did not know how to do this, I tried selecting by tag but there are other tags on the page (the 'a' tag) that do not link to what I want.

Examples of buttons to press:

<a href="stuMain.php?datasetID=31561">583</a>
<a href="stuMain.php?datasetID=31553">575</a>

Examples of what not to press:

<font color="red">579</font>
<a href="preview.php?datasetID=31557">View</a>

This is what I tried:

dataset = driver.find_element_by_tag_name('a')

dataset.click()
FloridaGuy
  • 141
  • 1
  • 10
  • Did you try using selenium in your code? please provide us with some of your codes – Tserenjamts Nov 06 '19 at 02:41
  • `driver.find_element_by_xpath('//a[@href="'+url+'"]')` URL is `stuMain.php?datasetID=31561` so you can click that links you wanted – Tserenjamts Nov 06 '19 at 02:50
  • that doesn't work, it gives the error no such element – FloridaGuy Nov 06 '19 at 02:54
  • That's just it says there is no such element :)) try looking more about `selenium` – Tserenjamts Nov 06 '19 at 02:55
  • It can be handled in multiple logics. Probably you can try with this first: https://stackoverflow.com/questions/21405267/xpath-using-regex-in-contains-function If it doesn't work we can figure it out. – Durga Prasad Behera Nov 06 '19 at 05:37
  • Do we have any idea which databaseId will be exists in the element what you have to click? If Yes, Write an xpath by taking databaseId as a parameter like below: //a[text()='${parameterName}']. By this way, we can write an dynamic xpath to click on any element which is tagged with 'a' and with that databaseId. – Sreenivasulu Nov 06 '19 at 09:31

1 Answers1

0

You can do something like:

a = driver.execute_script("""
  return [...document.querySelectorAll('a[href*="datasetID="]')].find(a => a.innerText.match(/^\d+$/))
""")

It might be simpler to get and filter those in python.

pguardiario
  • 53,827
  • 19
  • 119
  • 159