0

I am trying to use Selenium to click an element that's contained within a ::before selector, but every time I try to do so Selenium returns a NoSuchElementException. Looking at some related threads, it seems as though this is expected behavior for reasons I don't fully understand (see some seemingly related answers here: How to click on ::before element; https://groups.google.com/forum/#!topic/selenium-users/ZUi9cenA13o). Note that this section is not inside an iframe, which seems to be another common issue when people run into problems like this.

Here's the relevant section of HTML for the page I'm trying to use:

<div class="container">
    ::before
  <div id="ctl00_divPageContent" class="page-content center-block">
    <div class="col-xs-12 main remove-padding-sm add-padding-quater-sm-remove-padding-xs add-padding-quater-xs">
      <div id="ctl00_ContentPlaceHolder2_upView">
        <div class="col-xs-12 remove-padding-xs">
          <div class="pull-right">
            <div class="modal-dropdown pull-right">
              <a data-toggle="dropdown" tabindex="1">
                <i class="fa fa-ellipsis-v">
                    ::before
                </i>
              </a>
              <div class="dropdown-menu" role="menu">
                <a id="ctl00_ContentPlaceHolder2_lbnSelected" href="javascript:__doPostBack('ctl00$ContentPlaceHolder2$lbnSelected','')">Select</a>
                <a id="ctl00_ContentPlaceHolder2_lbnPdf" href="javascript:__doPostBack('ctl00$ContentPlaceHolder2$lbnPdf','')">
                  <em class="txt-underline">Print PDF</em>
                </a>

I'm trying to click on the second javascript link within the drop-down menu (i.e., the one with id="ctl00_ContentPlaceHolder2_lbnPdf"), but I haven't been able to get anything to work. In the second thread listed above, others seem to show that this requires executing some JavaScript, but I'm having trouble understanding what JavaScript I actually need to run to successfully do this, or how to execute it from within Python.

Any help would be greatly appreciated!

EDIT: Here's some of the code I've tried using:

# set up driver
driver = webdriver.Chrome()
... # omitting code here for navigating to the right page, etc.
# each of these returns a NoSuchElementException
driver.find_elements_by_xpath('//*[@id="ctl00_ContentPlaceHolder2_lbnPdf"]')
driver.find_element_by_css_selector('#ctl00_ContentPlaceHolder2_lbnPdf')
driver.find_element_by_link_text('Print PDF Resume')

Note that I'm pretty sure that I have navigated to the correct page, because I'm able to locate the div at the top of the block I've pasted using its xpath:

# does not return a NoSuchElementException
driver.find_element_by_xpath('//*[@id="aspnetForm"]/div[2]/div[1]/div')
  • There are no elements **inside** `::before` pseudo element! Simply try to locate required link by link text `"Print PDF"` – Andersson Oct 20 '17 at 05:18
  • Thanks, @Andersson. What happens when I do try and do that, though, is that it still returns a NoSuchElementException. Note that there are actually two ::before selectors within the HTML block. It seems to me at least that the element is inside the first of them contained within the container div. Is that not the case? – Nathan Stornetta Oct 20 '17 at 15:44
  • `::before` is not a WebElement, it's not a part of DOm, so it cannot have any descendant nodes. I guess it's just a timing issue. Try to `import time` and execute `time.sleep(5)` before locating element. Does it helps? – Andersson Oct 20 '17 at 16:22
  • Hmm... that's a good insight and does help me make a bit more sense of this. Unfortunately it's definitely not a timing issue. Even if I wait for all elements on the page to load, I can't locate the element regardless of the Selenium method I use to try and find it. – Nathan Stornetta Oct 20 '17 at 16:53
  • ok. You said that you want to click option from drop-down menu, right? Share exact code you use to perform the same (update the ticket with "edit" option) – Andersson Oct 20 '17 at 17:01
  • I've updated the ticket with some of the exact code I've used to try and locate the drop-down link element. – Nathan Stornetta Oct 21 '17 at 18:54
  • Did you click on something to expand drop-down menu? – Andersson Oct 21 '17 at 19:35
  • I've tried it both ways. My understanding (based on this [SO answer](https://stackoverflow.com/questions/7867537/selenium-python-drop-down-menu-option-value) had been that it wasn't necessary to do that first, so I was omitting that step here. When I try to click first on the drop-down with a step like this: `driver.find_element_by_xpath('//*[@id="ctl00_ContentPlaceHolder2_upViewResumeBook"]/div[1]/div/div[1]/a').click()` it returns the same `NoSuchElementException`. – Nathan Stornetta Oct 21 '17 at 21:27
  • In your case it is **mandatory** to make a click to expand drop-down. If you cannot make a click try `driver.implicitly_wait(10)` and then `driver.find_element_by_xpath('//a[@data-toggle="dropdown"]').click()` – Andersson Oct 22 '17 at 08:02
  • That's good to know. That does work, but if I watch the browser as it's doing that, it doesn't actually cause the dropdown to come down, and (seemingly) as a result, it still can't find the elements inside the dropdown afterwards. – Nathan Stornetta Oct 22 '17 at 20:56
  • can you share page url? – Andersson Oct 23 '17 at 04:50
  • I can't, unfortunately, since it's part of a private intranet. I'd be happy to provide some kind of screenshare as I do it, though, if that would help. – Nathan Stornetta Oct 23 '17 at 13:39

0 Answers0