It would be nice if someone knows how to select the checkbox using Selenium with Python.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
no_class = browser.find_element(By.XPATH, "//div[#id='icon-literary-collections']/following-sibling::a[1]")
no_class.click
and this is the HTML Part :
<div class="a-checkbox" style="">
<label for="checkbox-non--classifiable" style="">
<input id="checkbox-non--classifiable" type="checkbox" name="" value="" nodeid="non--classifiable" style="" class="">
<i class="a-icon a-icon-checkbox"></i>
<span class="a-label a-checkbox-label" style="">Non-Classifiable</span>
</label>
</div>
I want to select the checkbox "Non-classifiable" but i couldn't do that it tried I tried as id, name, link_text but could not detect what should be used?
You can try clicking on it by using its text in the xpath and using explicit wait so that the script waits till the element is present on the page.
You can do it like:
element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//span[text()='Non-Classifiable']")))
element.click()
You need 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
WebDriverWait(driver, 30).until(
EC.element_to_be_clickable((By.XPATH, "//span[#class='a-label a-checkbox-label']"))).click()
Javascript click:
checkBox=WebDriverWait(driver, 30).until(
EC.element_to_be_clickable((By.XPATH, "//span[#class='a-label a-checkbox-label']")))
driver.execute_script("arguments[0].click();", checkBox)
Note : please add below imports to your solution
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
Related
The whole html: https://dpaste.com/82768UN7D
There are several such elements:
<input type="file" name="images_responsiveimage_related-0-responsive_image" accept="image/*" id="id_images_responsiveimage_related-0-responsive_image"></p>
I want to find the last of them via Python's Selenium:
last_input_type_xpath = "//input[#type='file'][last()]"
input_element = self.driver.find_element_by_xpath(xpath)
I get NoSuchElementException.
Your xpath seems wrong.To get the last input file Try that
(//input[#type='file'])[last()]
Ideally you should wait for the element use webdriverwait
input_element=WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "(//input[#type='file'])[last()]")))
You need to import below libraries
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
I'm developing test automation and I want to click on element located in toolbar,I'm using python and selenium. Here is a code of website
<dl class="ToolbarDropdown_menu_2wY9X ToolbarDropdown_isHidden_3UaGr" tabindex="0" style=""
xpath="1"><dt><span class="ToolbarDropdown_title_1NBVn">New</span>
<span class="DropdownArrow_arrowIcon_dDzph DropdownArrow_down_3dlvo"></span>
</dt>
<dd class="ToolbarDropdown_item_nP-_M" style="">Dataset</dd>
<dd class="ToolbarDropdown_item_nP-_M">Project</dd>
</dl>
I need to click on element with text Dataset.
I locate element in this way
DATASET_BUTTON = (By.XPATH, '//dd[contains(text(),"Dataset")]')
And I want to perform action like that
self.driver.find_element(*VideosPageLocator.DATASET_BUTTON).click()
And there is no action but in terminal looks like my step has passed. Do you have any idea how to click on element in dropdown?
Maybe the action is done before DOM has loaded completely.
Try using WebDriverWait.
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
web_driver_wait = WebDriverWait(self.driver, 10)
element = web_driver_wait.until(EC.element_to_be_clickable((By.XPATH,'//dd[contains(text(),"Dataset")]')))
element.click()
To invoke click() on the element with text as Dataset you have to induce WebDriverWait for the element_to_be_clickable() and you can use the following Locator Strategy:
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//dl[starts-with(#class, 'ToolbarDropdown')]//dd[starts-with(#class, 'ToolbarDropdown_item') and text()='Dataset']"))).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
how to press the "f_agreements_all" button ?
<label for="f_agreements_all">
<input type="checkbox" id="f_agreements_all">
<span></span>
<span class="permText">I accept all</span>
</label>
Unfortunately, finding "f_agreements_all" button and clicking it doesnt work. Span covers the whole button17x17px, when f_agreements_all is 16x16 under it. Do you know possible way to click it?
Using vanilla JS one way to do it would be:
const checkbox = document.getElementById('f_agreements_all');
checkbox.click();
Quickly looking at the Python Selenium 2 WebDriver API docs, one can try:
driver.find_element_by_id("f_agreements_all").click()
Can you please try below xpath :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
button=WebDriverWait(browser, 10).until(
EC.element_to_be_clickable((By.XPATH, "//li[#class='formCheckbox agreements all-agreements']//span[1]")))
button.click()
To click on the element 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, "label[for='f_agreements_all']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[#for='f_agreements_all']"))).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
Can someone help me understand why my code fails to find the element by ID. Code below:
from selenium import webdriver
driver=webdriver.Firefox()
driver.get('https://app.waitwhile.com/checkin/lltest3/user')
element = driver.find_element_by_id("guestPhone")
Inspecting element shows the ID clearly.
<input type="tel" name="guestPhone" id="guestPhone" class="form-control ng-pristine ng-empty ng-invalid ng-invalid-phone-validator ng-invalid-required ng-touched" ng-model="form.model" ng-model-options="{ 'updateOn': 'default blur', 'debounce': { 'default': 350, 'blur': 0 } }" uib-typeahead="guest.phone for guest in form.onChange({value:$viewValue})" typeahead-min-length="6" typeahead-on-select="form.onSelect({guest:$item})" typeahead-select-on-exact="true" uib-tooltip="Please enter valid number. Include country code for non-US numbers" tooltip-trigger="'none'" tooltip-is-open="(form.guestForm.$submitted || form.guestForm.guestPhone.$touched) && form.guestForm.guestPhone.$invalid" tooltip-placement="bottom" ng-required="::form.required" phone-validator="US" placeholder="Mobile phone" title="Please enter a valid phone number" autocomplete="nope" next-on-enter="" aria-autocomplete="list" aria-expanded="false" aria-owns="typeahead-47-2884" required="required" style="">
P.S. I've also tried XPath and name as well. Still no luck.
You need to wait for the element to become visible on the page. You can tell this is loaded in dynamically because if you right-click on the page in chrome and view source you'll see there's no guestPhone element. It gets loaded in with javascript
Here's an example from http://isaacviel.name/make-web-driver-wait-element-become-visiable/:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox()
driver.get("http://somedomain/url_that_delays_loading")
try:
element = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.ID, "myDynamicElement"))
)
finally:
driver.quit()
You can try with web driver wait :
Code :
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(executable_path = r'D:/Automation/chromedriver.exe')
driver.maximize_window()
driver.get("https://app.waitwhile.com/checkin/lltest3/user")
wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.ID, 'guestPhone')))
element.send_keys('006867987')
</div>
<fieldset class="pi-search-form__footer">
<div class="pi-search-form__footer-cta" data-ng-show="stay.isStandardBooking()">
<button class="btn btn--primary" data-ng-click="submitSearch()">
<span data-ng-switch="ctaText">
<span data-ng-switch-when="checkavailability">Check availability</span>
<span data-ng-switch-when="searchnow">Search now</span>
<span data-ng-switch-default data-pi-track-click="SEARCH_AGAIN">Search</span>
</span>
</button>
</div>
This is my button code and I want to click "Check availability" button.
I use that code but it not works.
browser.find_elements_by_xpath(".//span[contains(text(), 'Check availability')]")
Here is the Answer to your Question:
As you want to click on a button so your xpath should be unique and should identify a single element. Hence instead of find_elements_by_xpath you should consider using find_element_by_xpath. To click on the Check availability button you can use the following line of code:
//ensure the imports
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
//other code
//click on Check availability button
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='btn btn--primary']//span[#data-ng-switch-when='checkavailability']")))
browser.find_element_by_xpath("//button[#class='btn btn--primary']//span[#data-ng-switch-when='checkavailability']").click()
Let me know if this Answers your Question.
it should be
browser.find_elements_by_xpath(".//span[contains(text(), 'Check availability')]")
and if it's not working use explicit wait.
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 30)
checkAvailability = wait.until(EC.presence_of_element_located((By.XPATH, ".//span[contains(text(), 'Check availability')]")))