How to click on elements blocked by pop up using selenium python? - python

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

Related

Selenium Automation Angular JS Button (Python) (Chrome)

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

How to handle dropdown without select in selenium python

Hello I would like to be able to change the value of "50 Profiles / Page" to "500 Profiles / Page", but the problem is that in the HTML there is no "Select" tag.
I tried doing this but it didn't work
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
url = 'https://www.personality-database.com/profile?pid=1&sort=hot'
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.maximize_window()
driver.implicitly_wait(30)
driver.get(url)
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="root"]/div/section/main/div[1]/div[2]/div/div[5]/ul/li[10]/div/div[1]/span[2][text()="500 Profiles / Page"]'))).click()
Here is the code The HTML code
<li class="rc-pagination-options">
<div class="rc-select rc-pagination-options-size-changer rc-select-single rc-select-show-arrow">
<span class="rc-select-arrow" unselectable="on" aria-hidden="true">
<span class="rc-select-arrow-icon"></span></span>
<div class="rc-select-dropdown rc-select-dropdown-placement-topLeft rc-select-dropdown-hidden">
<div role="listbox" id="rc_select_0_list">
<div aria-label="20 Profiles / Page" role="option" id="rc_select_0_list_0"
aria-selected="false">20</div>
</div>
<div class="rc-virtual-list" style="position: relative;">
<div class="rc-virtual-list-holder">
<div class="rc-virtual-list-holder-inner"
style="display: flex; flex-direction: column;">
<div aria-selected="false" class="rc-select-item rc-select-item-option"
title="20 Profiles / Page">
<div class="rc-select-item-option-content">20 Profiles / Page</div><span
class="rc-select-item-option-state" unselectable="on" aria-hidden="true"
style="user-select: none;"><span
class="rc-select-item-option-state-icon"></span></span>
</div>
<div aria-selected="false" class="rc-select-item rc-select-item-option"
title="500 Profiles / Page">
<div class="rc-select-item-option-content">500 Profiles / Page</div><span
class="rc-select-item-option-state" unselectable="on" aria-hidden="true"
style="user-select: none;"><span
class="rc-select-item-option-state-icon"></span></span>
</div>
...
</li>
First we need to close the pop-ups and then try to click on pagination options.
And using both Implicit wait and Explicit wait is not Recommended.
Try the following solution:
driver.get("https://www.personality-database.com/profile?pid=1&sort=hot")
wait = WebDriverWait(driver,30)
try:
# Close the footer add
wait.until(EC.element_to_be_clickable((By.XPATH,"//span[#id='ezmob-wrapper']/div/center/span/div/div/span"))).click()
# Scroll a distance so that the Cookie pop up appears and Close it
driver.execute_script("window.scrollBy(0,50);")
wait.until(EC.element_to_be_clickable((By.XPATH,"//button[#id='rcc-confirm-button']"))).click()
except:
print("no adds")
# click on the drop down option
pagination = wait.until(EC.element_to_be_clickable((By.XPATH,"//li[#class='rc-pagination-options']")))
pagination.click()
# Click on the 500 profiles
option = wait.until(EC.element_to_be_clickable((By.XPATH,"//div[#class='rc-virtual-list-holder-inner']//div[text()='500 Profiles / Page']")))
option.click()
First xpath to click dropdown:
//div[#class='rc-select rc-pagination-options-size-changer rc-select-single rc-select-show-arrow']
Second xpath to click the option for 500 pages:
//div[#class='rc-select-item-option-content']/self::div[text()='500 Profiles / Page']
Here is a cheatsheet for relative xpaths https://devhints.io/xpath
Please be aware that browsers use xpath 1.0 and selenium also only supports 1.0,
So some things like 'ends-with' won't work.

Python Selenium input date in ng datepicker

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

How to click on item in navigation bar on top of page using selenium python?

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

How to click on a Radio Button as per the HTML using Selenium and Python

I am trying to click on element with following html code:
<div class="delivery-pseudo-table">
<div class=" i-popup-lk user-address" id="userAddress">
<div class="address-container">
<div class="titlebar">
<h1 class="title"></h1>
</div>
<div id="userAddressContent"></div>
</div>
<div class="hide j-map map" id="addressMap"></div>
<div class="hide j-map-pickpoint postamat-map" id="pickpointMap"></div>
<div class="added-addres-loader"></div>
</div>
<ul class="delivery-method" data-jsv-df=""><li class="selfDelivery selectDeliveryWay active" data-jsv="#34_#35_" title="">
<script type="jsv#37_"></script>
<label><input autocomplete="off" checked="checked" data-id="2" name="orderDetails.DeliveryWay_q" type="radio" value="Self"/>Самовывоз</label>
<script type="jsv/37_"></script>
</li><li class="courierDelivery selectDeliveryWay" data-jsv="/35_#38_" title="">
<script type="jsv#39_"></script>
<label><input autocomplete="off" data-id="1" name="orderDetails.DeliveryWay_q" type="radio" value="Courier"/>Доставка курьером</label>
<script type="jsv/39_"></script>
</li><li class="wbpostamatDelivery selectDeliveryWay" data-jsv="/38_#40_" title="">
<script type="jsv#41_"></script>
<label><input autocomplete="off" data-id="16" name="orderDetails.DeliveryWay_q" type="radio" value="WbPostamat"/>Постамат</label>
<script type="jsv/41_"></script>
</li></ul>
I need to click on button with value "Courier"
And I am trying to do it with following Python code:
elem = browser.find_element_by_css_selector("input[type='radio'][#value='Courier']")
elem.click()
(I've also tried to find by xpath)
But it says that the element is invisible. Are the any ways to click on such element?
Try to use this xPath:
//input[#type='radio' and #value='Courier']
also try to use WebDriverWait:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[#type='radio' and #value='Courier']"))).click()
Note: you have to add some imports:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
WebDriverWait statement is for waiting at least 10 seconds until element will be clickable and only then clicks on it.
PS: more information about WebDriverWait can be found here.
EDIT: try to execute JavaScript:
radio_btn = driver.find_element_by_xpath("//input[#type='radio' and #value='Courier']")
driver.execute_script("arguments[0].click();", radio_btn)
As per the HTML you have shared the <ul> tag contains a lot of JavaScripts so to invoke click() on the desired element you have to induce WebDriverWait for the element to be clickable and you can use either of the following solutions:
CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "ul.delivery-method li.courierDelivery.selectDeliveryWay input[value='Courier']"))).click()
XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//ul[#class='delivery-method']//li[#class='courierDelivery selectDeliveryWay']//input[#value='Courier']"))).click()

Categories