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')