Code trials:
import selenium
from selenium import webdriver
PATH = 'C:\Program Files (x86)\chromedriver.exe'
driver = webdriver.Chrome(PATH)
driver.get("https://oncf-voyages.ma")
origin = driver.find_elements_by_xpath('/html/body/div[1]/section/div[1]/div[2]/main/div[1]/div/div/div/div/div[1]/div/div[2]/div[1]/div/div/div[1]/div[1]/div[2]/div/div[1]/div/div/div/div/div[2]/div/input')
origin.send_keys("something")
origin.send_keys(Keys.RETURN)
I used the python code above to try and practice some basic operations with selenium but I can't access an input clause
Here's the website that I'm working on : https://www.oncf-voyages.ma/
This is a part of the html code of the website:
<div class="ant-col SearchForm_col ant-col-xs-24 ant-col-md-24 ant-col-xl-24 ant-col-xxl-24" style="padding-left: 15px; padding-right: 15px;"><label class="SearchForm_label">Ma gare de départ</label><div class="InputWrapper"><div id="origin" class="SelectPicker SelectWidget ant-select ant-select-enabled"><div class="ant-select-selection
ant-select-selection--single" role="combobox" aria-autocomplete="list" aria-haspopup="true" aria-controls="b5b887d5-0fcf-488c-dc1a-9dca53b48787" aria-expanded="false" tabindex="0"><div class="ant-select-selection__rendered"><div unselectable="on" class="ant-select-selection__placeholder" style="display: block; user-select: none;">Ma gare de départ</div><div class="ant-select-search ant-select-search--inline" style="display: none;"><div class="ant-select-search__field__wrap"><input id="origin" autocomplete="off" class="ant-select-search__field" value=""><span class="ant-select-search__field__mirror"> </span></div></div></div><span class="ant-select-arrow" unselectable="on" style="user-select: none;"><i aria-label="icon: down" class="anticon anticon-down ant-select-arrow-icon"><svg viewBox="64 64 896 896" focusable="false" class="" data-icon="down" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M884 256h-75c-5.1 0-9.9 2.5-12.9 6.6L512 654.2 227.9 262.6c-3-4.1-7.8-6.6-12.9-6.6h-75c-6.5 0-10.3 7.4-6.5 12.7l352.6 486.1c12.8 17.6 39 17.6 51.7 0l352.6-486.1c3.9-5.3.1-12.7-6.4-12.7z"></path></svg></i></span></div></div></div></div>
The <input> with you are trying to locate with the xpath:
/html/body/div[1]/section/div[1]/div[2]/main/div[1]/div/div/div/div/div[1]/div/div[2]/div[1]/div/div/div[1]/div[1]/div[2]/div/div[1]/div/div/div/div/div[2]/div/input
is within it's ancestor <div> with style attribute set as display: none;
So Selenium won't be able to interact with the element unless it's visible, enabled and interactable(clickable).
Related
So I have started messing around with Selenium in python and I am just not able to figure this xpath out.
<div title class="popupBox" style="left: 0px; to p: 490px;">
<svg class="cp Drag" viewbox="0 0 24 24" style="height: 10px; width: 10px;">...</svg>
<div title class="eqBox">
<div title class="ib cp">
<img title src="src.svg">
<div title class="bred"></div>
<div title class="ds2">+1</div>
<div title class="ds3 IQ">TOP</div>
</div>
</div>
I need to be able to locate the TOP and get the result of TOP.
Hopefully that makes sense and I provided what is needed.
Try:
driver.find_element(By.XPATH, '//*[#class="ds3 IQ"]').text.splitlines[-1]
I have the following HTML code
<ul id="vmStateDropDown" class="dropdown-content active" style="white-space: nowrap; position: absolute; top: 83px; left: 264px; opacity: 1; display: block;"><li class="waves-effect waves-default" style="display: block;"><a class="" style="cursor: pointer;"><i class="material-icons left" style="cursor: pointer;">directions_run</i><span>Turn On</span></a></li><li class="waves-effect waves-default" style="display: block;"><a class="disabled" disabled="" style="cursor: pointer;"><i class="material-icons left" style="cursor: pointer;">power_settings_new</i><span>Turn Off</span></a></li><li class="waves-effect waves-default" style="display: block;"><a class="disabled" disabled="" style="cursor: pointer;"><i class="material-icons left" style="cursor: pointer;">loop</i><span>Restart</span></a></li></ul>
I need to click in the list item Turn On. Here is the code I have so far.
turn_vmon = driver.find_elements(By.XPATH,'/html/body/div[4]/main/div/div[3]/div[3]/div[1]/ul/li[1]')
When I print the text for turn_vmon variable, I can see the option which I need. However I am unable to figure out how to click on the list item.
Can someone please help?
To click on the element with text as Turn On you can use either of the following Locator Strategy:
Using xpath:
driver.find_element(By.XPATH, "//span[text()='Turn On']").click()
Ideally to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategy:
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Turn On']"))).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
This question already has answers here:
How to select a drop-down menu value with Selenium using Python?
(18 answers)
Closed 1 year ago.
I'm using python with selenium and it doesn't find the dropdown. I don't have a Select tag in DOM. Any ideas ?
P.S the developers added DATA QA for me
Here is the relevant HTML :
<div class="MuiGrid-root contact-us-styles__StyledGridContainerForTextFields-sc-1xqduqn-8 dSYioi MuiGrid-item MuiGrid-grid-xs-12 MuiGrid-grid-sm-12 MuiGrid-grid-md-12">
<div>
<label class="MuiFormLabel-root MuiInputLabel-root InputLabel__StyledInputLabel-sc-1ryqn8h-0 hUmuWj MuiInputLabel-animated" for="component-simple">Bitte wählen Sie Ihre Bestellart aus *</label>
<div class="MuiFormControl-root Dropdown__StyledFormControl-sc-u37r21-0 JRpKK" sx="[object Object]">
<div class="MuiInputBase-root MuiOutlinedInput-root Dropdown__StyledSelect-sc-u37r21-1 gBjzLJ MuiInputBase-formControl">
<div class="MuiSelect-root MuiSelect-select MuiSelect-selectMenu MuiInputBase-input MuiOutlinedInput-input" tabindex="0" role="button" aria-haspopup="listbox">
<span>​</span>
</div>
<input aria-hidden="true" tabindex="-1" class="MuiSelect-nativeInput" data-qa-id="orderType" value="">
<svg class="MuiSvgIcon-root MuiSelect-icon" focusable="false" viewBox="0 0 24 24" aria-hidden="true">
<path d="M7 10l5 5 5-5z"></path>
</svg>
<fieldset aria-hidden="true" class="PrivateNotchedOutline-root-150 MuiOutlinedInput-notchedOutline" style="padding-left: 8px;">
<legend class="PrivateNotchedOutline-legend-151" style="width: 0.01px;">
<span>​</span>
</legend>
</fieldset>
</div>
</div>
</div>
</div>
P.S the developers added DATA QA for me
Then you should be able to use attribute selector like
input[data-qa-id]
Please launch JavaScript console in your browser and check if
document.querySelector("input[data-qa-id]")
does get what you want. If yes then it should also work with selenium's find_element_by_css_selector
Using python and selenium to select an option in a combobox drop down ng select. I want to select the 'MALE' option in a dropdown select. I am trying to use XPath.
The code I use:
driver.find_element_by_id("sex_0").click()
driver.find_element_by_xpath("//*[contains(text(), 'MALE')]").click()
Copy and pasted the HTML:
Please Select OptionFEMALEMALE
Inspect element of the HTML:
<ng-select _ngcontent-uau-c14="" bindlabel="cdDescr" bindvalue="cd" class="custom ng-select ng-select-single ng-select-searchable ng-untouched ng-pristine ng-valid ng-select-opened ng-select-bottom" placeholder="Please Select Option" role="listbox" title="Please Select Option" id="sex_0">
<div class="ng-select-container">
<div class="ng-value-container"><div class="ng-placeholder">Please Select Option</div><!----><!---->
<div class="ng-input" style="top: 0px;"><input role="combobox" type="text" autocomplete="ac08684b8af8" autocorrect="off" autocapitalize="off" aria-expanded="true" style="position: relative; top: 0px; left: -10px; padding-left: 10px; border: none !important; height: 45px; width: 468px;" aria-owns="ac08684b8af8" aria-activedescendant="ac08684b8af8-0"></div></div><!----><!---->
<span class="ng-arrow-wrapper"><span class="ng-arrow"></span></span></div><!---->
<ng-dropdown-panel class="ng-dropdown-panel ng-select-bottom" id="ac08684b8af8" style="opacity: 1;"><!---->
<div class="ng-dropdown-panel-items scroll-host"><div></div>
<div><!----><!---->
<div class="ng-option ng-option-marked" role="option" aria-selected="false" id="ac08684b8af8-0"><!----><!----><span class="ng-option-label">FEMALE</span></div>
<div class="ng-option" role="option" aria-selected="false" id="ac08684b8af8-1"><!----><!----><span class="ng-option-label">MALE</span></div>
<!----><!----><!----><!----></div></div><!----></ng-dropdown-panel></ng-select>
The problem:
It will select FEMALE instead of MALE because the XPath text "FEMALE" contains the text "MALE". I assume that the 'id="ac08684b8af8"' is randomly generated with each use, which means I can't use Selenium find element by id. I tried select using selenium but I get the error: "element of type ng select not select". Hence I used XPath. The "ng-option-marked" is just a feature when the cursor hovers above the dropdown selection.
How should I solve this problem?
Answer is in the comments by KunduK.
driver.find_element_by_xpath("//*[text()='MALE']").click()
https://gyazo.com/9887635aa04d723758ba17b3413f72e1 - the html
the one named class="radio inline bundle-cta" is the one i want.
<label class="radio inline bundle-cta">
<input data-bind="checked: activeBundle" id="BundleType" name="BundleType" type="radio" value="Default">
Nej tak, jeg vælger en basisannonce uden tilvalg
<span class="uneditable-input price">Kr. <span data-price="0">0</span>,-</span>
<span class="bundle-cta-button">Vælg basisannonce</span>
</label>
error msg, when trying to click it:
Element is not clickable at point (604, 698). Other element would receive the click: <p style="line-height: 20px; margin: 0px 40px 0px 15px; padding: 0px;">...</p>
(Session info: chrome=54.0.2840.71)
(Driver info: chromedriver=2.25.426923 (0390b88869384d6eb0d5d09729679f934aab9eed),platform=Windows NT 10.0.14393 x86_64)
code I used to try to click it:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.class, "bundle-cta-button"))
# vælg gratis pakke
browser.find_element_by_class_name('bundle-cta-button').click()
sleep(0.75)
browser.find_element_by_xpath('//*[#id="content"]/div[2]/div/div/form/div/button').click()
Try to change spaces into dots, and find by CSS_selector like below:
browser.find_element_by_css_selector("radio.inline.bundle-cta").click()