I'm trying to automate a website to automatically make user profiles. Have run into a NG button that I have not be able to select. I have tried XPATH, CSS, and Class Name. None to these seem to work. I keep getting "NoSuchElementException" with:
By.XPATH, '//button[normalize-space()="Create New User"]'
and
By.CSS_SELECTOR, '.newUser.ng-scope'
When I try to use Webdriverwait, I get a time out error.
Code trials:
import time
from webbrowser import Chrome
from xml.etree.ElementPath import find
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
driver = webdriver.Chrome()
driver.get('https://power.dat.com')
chrome_optinons = webdriver.ChromeOptions()
chrome_optinons.add_argument('--headless')
chrome_optinons.add_argument('window-size=1920x1080')
username = driver.find_element(By.XPATH, '//*[#id="mat-input-1"]')
username.send_keys('')
password = driver.find_element(By.XPATH, '//*[#id="mat-input-0"]')
password.send_keys('')
#(time.sleep(3))
loginbutton = driver.find_element(By.XPATH, '//*[#id="submit-button"]')
loginbutton.click()
(time.sleep(5))
profiledrop = driver.find_element(By.XPATH, '//*[#id="user-salutation"]')
profiledrop.click()
Adminbutton = driver.find_element(By.CSS_SELECTOR, '#userPreferencesUl > li:nth-child(5) > a')
Adminbutton.click()
# (time.sleep(10))
NewUsrBtn = driver.find_element(By.XPATH, '//button[normalize-space()="Create New User"]')
NewUsrBtn.click()
#WebDriverWait(driver , 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.newUser.ng-scope'))).click()`
The HTML
<header>
<ul>
<!-- ngRepeat: product in office.products --><!-- ngIf: product.seatCount --><li ng-repeat="product in office.products" ng-if="product.seatCount" class="ng-scope">
<span class="product ng-binding">National Account: Combo Premium Pooling Subscription</span> <span class="count ng-binding">999999841</span> seats out of <span class="total ng-binding">999999999</span> available
</li><!-- end ngIf: product.seatCount --><!-- end ngRepeat: product in office.products --><!-- ngIf: product.seatCount --><li ng-repeat="product in office.products" ng-if="product.seatCount" class="ng-scope">
<span class="product ng-binding">National Account: DAT Connexion Subscription</span> <span class="count ng-binding">999999181</span> seats out of <span class="total ng-binding">999999999</span> available
</li><!-- end ngIf: product.seatCount --><!-- end ngRepeat: product in office.products --><!-- ngIf: product.seatCount --><li ng-repeat="product in office.products" ng-if="product.seatCount" class="ng-scope">
<span class="product ng-binding">National Account: Power Broker Subscription</span> <span class="count ng-binding">999999182</span> seats out of <span class="total ng-binding">999999999</span> available
</li><!-- end ngIf: product.seatCount --><!-- end ngRepeat: product in office.products -->
</ul>
<h3 class="ng-binding">Total Quality Logistics INC</h3>
<p>Need help? Customer support is available 4AM-6PM Pacific weekdays and 5AM-12PM Pacific Saturdays at
<span class="supportPhone">1-800-848-2546.</span></p>
<!-- ngIf: isTrustedAdmin() -->
<button class="newUser ng-scope" type="button" ng-if="isTrustedAdmin()" ng-click="onNewUser();" ng-disabled="user.isInEditMode">
Create New User
</button><!-- end ngIf: isTrustedAdmin() -->
<button class="manageGroups" type="button" ng-click="onManageGroups();">
Manage Groups
</button>
</header>
I also attached a screen shot of more of the HTML if needed
The desired element is an Angular element, so to click on the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.newUser.ng-scope[ng-if^='isTrustedAdmin'][ng-click^='onNewUser']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='newUser ng-scope' and contains(., 'Create New User')][starts-with(#ng-if, 'isTrustedAdmin') and starts-with(#ng-click, 'onNewUser')]"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Update
As a last resort to to click using execute_script() as follows:
Using CSS_SELECTOR:
driver.execute_script("arguments[0].click();", driver.find_element(By.CSS_SELECTOR, "button.newUser.ng-scope[ng-if^='isTrustedAdmin'][ng-click^='onNewUser']"))
Using XPATH:
driver.execute_script("arguments[0].click();", driver.find_element(By.XPATH, "//button[#class='newUser ng-scope' and contains(., 'Create New User')][starts-with(#ng-if, 'isTrustedAdmin') and starts-with(#ng-click, 'onNewUser')]"))
Related
I am trying to click a button on a website in Python using Selenium. The html for the button I want looks like this:
<a onclick="exportGridExcel()"><span class="accordion-download"><i class="fa fa-file-excel-o" title="Download Excel" aria-hidden="true"></i></span></a>
A more expanded version of the html is:
<div class="lang-switch-wrapper lang-switch-inverse-wrapper">
<div class="lang-switch">
<ul>
<li class="dropdown">
<a href="#" class="dropdown-toggle lang-lable" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
<span class=" hidden-xs">English</span>
<span class="hidden-lg hidden-md hidden-sm">EN</span>
<img src="/etc/designs/wbrrdesign/clientlibs-wbrredsign/img/angle-down-gray.svg" alt="dropdown"></a>
<ul class="dropdown-menu dropdown-item">
<li>Español</li>
</ul>
</li>
</ul>
</div>
</div>
Then some other stuff before going to the button group and button I want:
<div class="button-group">
<button onclick="onModifyQuery()" type="button" class="btn">Modify Query</button>
<a onclick="exportGridExcel()"><span class="accordion-download"><i class="fa fa-file-excel-o" title="Download Excel" aria-hidden="true"></i></span></a>
<a title="Undo to column removal" onclick="restoreColumn()" class="toggle-btn primary-light-blue-btn"><i class="fa fa-circle-o-notch" aria-hidden="true"></i></a>
</div>
Part of my confusion is inexperience with this html with multiple classes and "i class".
EDIT:
If I try something like:
WebDriverWait(driver,300).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[onclick='exportGridExcel()']"))).click()
Then I get the error:
ElementClickInterceptedException: element click intercepted: Element <a onclick="exportGridExcel()">...</a> is not clickable at point (772, 11). Other element would receive the click: <div class="lang-switch-wrapper lang-switch-inverse-wrapper">...</div>
The problem is that your page is automatically scrolled up and the excel download button is probably hidden by the top banner that contains the language selector. When Selenium tries to click on the excel download button, it finds the language selector instead.
I would suggest you to scroll the page up till the whole data table is visible
You can use something like this. Press HOME key to go to top of the page
from selenium.webdriver.common.keys import Keys
...
...
element = WebDriverWait(driver,300).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[onclick='exportGridExcel()']")))
element.send_keys(Keys.CONTROL + Keys.HOME)
element.click()
Ideally clicking on the element <a onclick="exportGridExcel()"> as per your code trials inducing WebDriverWait for the elementToBeClickable() should have worked.
However, there seems to be a decendant <span>. So you can use either of the following locator strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[onclick^='exportGridExcel'] > span.accordion-download"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[starts-with(#onclick, 'exportGridExcel')]/span[#class='accordion-download']"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
In another approach you can also use JavascriptExecutor as follows:
Using CSS_SELECTOR:
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[onclick^='exportGridExcel'] > span.accordion-download")))
driver.execute_script("arguments[0].click();", element)
Using XPATH:
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[starts-with(#onclick, 'exportGridExcel')]/span[#class='accordion-download']")))
driver.execute_script("arguments[0].click();", element)
I am trying to input a date into the text portion of a datepicker and send_keys is not working.
Here is the html:
<date-picker _ngcontent-bdp-c11="" _nghost-bdp-c3="">
<div _ngcontent-bdp-c3="" class="datePicker formDiv-header datePickerContainer">
<div _ngcontent-bdp-c3="" class="formLabel-header">
<!---->
<label _ngcontent-bdp-c3="" for="dateInput1">Search Date:</label>
<!---->
</div>
<div _ngcontent-bdp-c3="" class="formField-header" style="padding:0;">
<div _ngcontent-bdp-c3="" class="input-group date" id="dateTimePicker1">
<input _ngcontent-bdp-c3="" class="form-control formField-header" maxlength="10" type="text">
<span _ngcontent-bdp-c3="" class="input-group-addon"><span _ngcontent-bdp-c3="" class="glyphicon glyphicon-calendar">
</span>
</span>
</div>
</div>
</div>
</date-picker>
<search-button _ngcontent-bdp-c11="" _nghost-bdp-c4="">
<button _ngcontent-bdp-c4="" class="selectionButton" type="button"> Search
</button></search-button>
Here is my code:
dateInputfield = driver.find_element_by_xpath('// *[ # id = "dateTimePicker1"] / input')
# the dateInputfield: <selenium.webdriver.remote.webelement.WebElement (session="6911b2bc422dac02d926b3b36429e8de", element="0.8362094047920967-3")>
dateInputfield.send_keys('01/15/2021')
searchbt = driver.find_element_by_xpath('//*[#id="filters"]/search-button/button')
searchbt.click()
Alternatively, I have tried to click the calendar icon, which actually opens the calendar for selection, but I don't know how to navigate to a given date in order to select it.
calicon = driver.find_element_by_xpath('//*[#id="dateTimePicker1"]/span/span')
calicon.click()
Any help would be appreciated.
To send the date characters to the element you can use either of the following Locator Strategies:
Using css_selector:
driver.find_element(By.CSS_SELECTOR, "div.input-group.date#dateTimePicker1 > input.form-control.formField-header").send_keys('01/15/2021')
Using xpath:
driver.find_element(By.XPATH, "//div[#class='input-group date' and #id='dateTimePicker1']/input[#class='form-control formField-header']").send_keys('01/15/2021')
Ideally, to send the date characters to the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.input-group.date#dateTimePicker1 > input.form-control.formField-header"))).send_keys('01/15/2021')
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='input-group date' and #id='dateTimePicker1']/input[#class='form-control formField-header']"))).send_keys('01/15/2021')
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
I want to click open the options in the menu on the left but they are blocked by the pop-up. How can I do that?
here is what I have tried:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
chromedriver = "/usr/share/chromedriver/chromedriver"
driver = webdriver.Chrome(chromedriver)
driver.get("https://student.amizone.net")
driver.find_element(By.NAME, "_UserName").send_keys("username")
driver.find_element(By.NAME, "_Password").send_keys("password")
driver.find_element(By.CSS_SELECTOR, "#loginform .login100-form-btn").click()
driver.implicitly_wait(10)
#11 | click | id=ModalPopAmityHostel |
driver.find_element(By.ID, "ModalPopAmityHostel").click()
# 11 | click | id=StudentSatisfactionPop | |
driver.find_element(By.ID, "StudentSatisfactionPop").click()
this code closes the 1st pop-up but doesn't close the 2nd pop-up. In the code first I login to the website https://student.amizone.net i have not shown my username and password(obviously). after that driver.find_element(By.ID, "ModalPopAmityHostel").click() is supposed to click outside the pop-up which closes the pop-up. similarly driver.find_element(By.ID, "StudentSatisfactionPop").click() is supposed to close the 2nd pop-up.
this is the snippet of html code of the pop up elements:
<form action="/PopUp/Home/SANGATHANQuizpopupSave" data-ajax="true" data-ajax-loading="#lodingDiv" data-ajax-method="POST" data-ajax-mode="replace" data-ajax-success=" $('#SANGATHANQuizpop').modal('hide');$('.modal-backdrop').remove();$(document.body).removeClass('modal-open');alertify.alert('Successfully Saved.'); " data-ajax-update="#Div_Partial" id="form0" method="post"></form>
<div id="StudentSatisfactionPop" class="modal fade in" role="dialog" aria-hidden="false" style="display: block; padding-right: 15px;">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">STUDENT SATISFACTION SURVEY </h4>
</div>
<div class="modal-body">
<div>
<p>
<b>Dear Mr MANIK RAINA </b>
</p>
<p>
Please tell us about you! Office of Research, Planning & Statistical Services, Amity University Uttar Pradesh is conducting a survey of its students. This survey asks your opinion on many items relevant to examining the impact of college. It asks about your transition to university, your academic habits and experiences, your interaction with peers and faculty, your involvement in campus activities and programs, and how you spend your time.
</p>
<p>Results from this survey will be used by us to understand and improve your experiences. Data received from your responses will be analyzed and interpreted batch wise only, and at no time the responses be linked with an individual. Data will be used for revision and improvement of resource planning for better learning experiences of students of future batches. Be assured that your responses will remain confidential.</p>
<p>We would very much appreciate you taking a few moments to fill out the Survey Questionnaire.</p>
<p><b>
To Start the survey :
<a data-ajax="true" data-ajax-begin="$('#sidebar').removeClass('display');" data-ajax-loading="#lodingDiv" data-ajax-mode="replace" data-ajax-success=" $('#StudentSatisfactionPop').modal('hide');$('.modal-backdrop').remove();$(document.body).removeClass('modal-open');" data-ajax-update="#Div_Partial" href="/Survey/StudentSatisfaction" id="12" rel="0">Click Here</a>.
</b></p>
</div>
</div>
<div class="modal-footer">
<button type="button" id="btnClosevoter" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div> <script>
$(document).ready(function () {
$('#StudentSatisfactionPop').modal('show');
});
</script>
<script>
$(document).ready(function () {
$('#ModalPopAmityHostel').modal('show');
});
</script>
<div id="ModalPopAmityHostel" class="modal fade in" role="dialog" aria-hidden="false" style="display: block;">
<div class="modal-dialog " style="z-index:104546464; ">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Amity Hostel</h4>
</div>
<div class="modal-body">
<p class="text-center">
“A few hostel seats (A/C and Non A/C) are available for allocation. Students desirous of availing the seats may apply on Amizone and also contact hostel office. Allocation of seats will be done on ‘first come, first served’ basis.”
</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>
<h4 class="modal-title"></h4>
</div>
</div>
</div>
</div>
You can close popups by clicking on close buttons. In your code you click to the div instead of close buttons, that's why popups do not close (see correct locators for the close buttons).
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
chromedriver = "/usr/share/chromedriver/chromedriver"
driver = webdriver.Chrome(chromedriver)
wait = WebDriverWait(driver, 10)
driver.get("https://student.amizone.net")
driver.find_element(By.NAME, "_UserName").send_keys("username")
driver.find_element(By.NAME, "_Password").send_keys("password")
driver.find_element(By.CSS_SELECTOR, "#loginform .login100-form-btn").click()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#ModalPopAmityHostel button.btn"))).click()
driver.execute_script("arguments[0].click()", driver.find_element_by_css_selector("#StudentSatisfactionPop button.btn"))
# wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#StudentSatisfactionPop button.btn"))).click()
As the <button> tags with text as Close are with in Modal Dialog Box so to locate and click() on the desired elements you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get('https://student.amizone.net')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "form#loginform input[name='_UserName']"))).send_keys("7071804")
driver.find_element_by_css_selector("form#loginform input[name='_Password']").send_keys("62ae6f")
driver.find_element_by_css_selector("form#loginform button[type='submit']").click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#ModalPopAmityHostel div.modal-footer button.btn.btn-primary[data-dismiss='modal']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#StudentSatisfactionPop div.modal-footer button.btn.btn-default[data-dismiss='modal']"))).click()
Using XPATH:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get('https://student.amizone.net')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//form[#id='loginform']//input[#name='_UserName']"))).send_keys("7071804")
driver.find_element_by_xpath("//form[#id='loginform']//input[name='_Password']").send_keys("62ae6f")
driver.find_element_by_xpath("//form[#id='loginform']//button[type='submit']").click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#id='ModalPopAmityHostel']//div[#class='modal-footer']//button[#class='btn btn-primary' and #data-dismiss='modal']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#id='StudentSatisfactionPop']//div[#class='modal-footer']//button[#class='btn btn-default' and #data-dismiss='modal']"))).click()
i am relatively new to Selenium , I am working on a web browser automation project and one of the action is to pick a element from an drop down menu please find below the html code.
<span id="export_menu" class="ui-button drop-down export-menu" tabindex="0" role="application">
<span class="menu_text">Export</span>
<span class="drop-down-menu ui-icon ui-icon-triangle-1-s"></span>
<ul class="export-actions"><li><header>Export Report</header>
<ul><li class="menu-action"><input type="button" value="CSV" class="button ui-button ui-widget ui-state-default ui-corner-all" id="export_csv" data-format="csv" role="button" aria-disabled="false"></li></ul>
<ul><li class="menu-action"><input type="button" value="PDF" class="button ui-button ui-widget ui-state-default ui-corner-all" id="export_pdf" data-format="pdf" role="button" aria-disabled="false"></li></ul>
<ul><li class="menu-action"><input type="button" value="Schedule Export" class="button ui-button ui-widget ui-state-default ui-corner-all" id="schedule" role="button" aria-disabled="false"></li></ul></li></ul>
</ul>
</span>
I tried following on Python, which give out error as below
driver.find_element_by_id("export_menu").click()
driver.find_element_by_id("export_csv").click()
selenium.common.exceptions.ElementNotInteractableException: Message: Element could not be scrolled into view
after some research I also tried following, which simply timeouts
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="export_csv"]'))).click()
requesting help!
To pick the element with value as CSV from an drop-down-menu you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following solutions:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.ui-button.drop-down.export-menu#export_menu"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "ul.export-actions li.menu-action > input.button.ui-button.ui-widget.ui-state-default.ui-corner-all#export_csv"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[#class='ui-button drop-down export-menu' and #id='export_menu']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//ul[#class='export-actions']//li[#class='menu-action']/input[#class='button ui-button ui-widget ui-state-default ui-corner-all' and #id='export_csv']"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
You can find a relevant discussion in How to select an option from a dropdown of non select tag?
Try first clicking the <ul>:
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH, '//*[#class="export-actions"]'))).click()
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="export_csv"]'))).click()
I am trying to test a web page using selenium python. All is working well but issue is encountering when clicking on navbar item
I have used:
driver.find_element_by_xpath('./li/a[. = "Log in"]')
Also have used:
driver.find_element_by_link_text('Log in')
Nothing got luck !!
The code snippet:
<div class='container'>
<div class='navigationbar__header'>
<a class='navigationbar__header__logo tracking-link' data-link-name='logo' href='/' target='_self'>
<div id='hired-brand'>HIRED</div>
</a>
</div>
<div class='navigationbar__toggle'>
<div class='navigationbar__toggle__element'>
<img alt='Menu' class='icon icon--sandwich' src='data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7'>
</div>
<input class='navigationbar__toggle__helper' type='checkbox'>
<ul class='navigationbar__navigation'>
<li class="navigationbar__item "><a class="sm-ml0 tracking-link" data-link-name="employers_page" target="_self" href="/employers">For Employers</a></li>
<li class="navigationbar__item "><a class="sm-ml0 tracking-link" data-link-name="success_stories" target="_self" href="/success-stories">Success Stories</a></li>
<li class="navigationbar__item "><a class="sm-ml0 tracking-link" data-link-name="employers_resources" target="_self" href="/employers/resources">Resources</a></li>
<li class="navigationbar__item "><a class="text-medium sm-ml0 tracking-link" data-link-name="login" target="_self" href="/login">Log in</a></li>
<div class='xs-ptb1 xs-prl1 md-ptb0 md-inline-block'><li class="navigationbar__item "><a class="button button--primary tracking-link" data-link-name="signup" target="_self" href="/signup">Sign Up</a></li></div>
</ul>
</div>
</div>
</nav>
This code is visible on page inspect easily. Anyone know the better way to interact with it?
To click() on the link with text as Log in within the website you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "li.navigationbar__item a[data-link-name='login'][href='/login']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//li[#class='navigationbar__item ']/a[#data-link-name='login' and #href='/login']"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Browser Snapshot:
Update
As an alternative you can use execute_script() as follows:
Using CSS_SELECTOR:
driver.execute_script("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "li.navigationbar__item a[data-link-name='login'][href='/login']"))))
Using XPATH:
driver.execute_script("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//li[#class='navigationbar__item ']/a[#data-link-name='login' and #href='/login']"))))
Ok so I checked the site.
The problem is that when the window size is small there is toggle for navigation that you need to click first.
try this
from selenium.common.exceptions import NoSuchElementException
try:
login_button = driver.find_element_by_link_text('Log in')
login_button.click()
except NoSuchElementException:
nav_bar_toggle = driver.find_element_by_class_name(
'navigationbar__toggle__helper'
)
nav_bar_toggle.click()
login_button = driver.find_element_by_link_text('Log in')
login_button.click()