Python selenium finding a dropdown [duplicate] - python

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>&ZeroWidthSpace;</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>&ZeroWidthSpace;</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

Related

Python Selenium deal with an angucomplete element

I have a selenium task at hand and the input element I'm trying to send_keys gives me an ElementNotInteractableException. figured that my input resides in an angucomplete element. well, I'm not new to selenium but can't find a solution for this so appreciate any idea or instruction for this
<angucomplete placeholder="" searchfields="" pause="400" set-focus="tpFocus" selectedobject="tpSelected" titlefield="label" inputclass="form-control form-control-small" matchclass="highlight" api="tpApi" disable="disableSearchBox" stockdetails="stockdetails"
class="ng-isolate-scope">
<div class="tp-re angucomplete-holder tp-width">
<div id="fulltextContainer" style="display: none" class="symbol-info tp-etc">
</div>
<input id="txt_search" class="search-box tp-co-1 tp-pa-rl-5 tp-re tp-bo-bo" type="text" placeholder="" onmouseup="this.select();" autocomplete="off" />
<div id="auto-list-container" class="auto-list-container tp-bg-2 tp-co-1 tp-bo-4 tp-bo tp-width angucomplete-dropdown" style="min-width: 300px; display: none">
<div style="display: none" id="loading" class="tp-h-35 tp-pa-rl-10 angucomplete-searching"></div>
<div style="display: none" id="norecord" class="tp-35 tp-pa-rl-10 angucomplete-searching"></div>
<div id="list_dropdown">
</div>
</div>
</div>
</angucomplete>
Use Javascript to set the value attribute of the input field.
For example:
elem = driver.find_element_by_tag_name("input")
value = "The text you want to put"
driver.execute_script("arguments[0].setAttribute('value', '{}')".format(value), elem)

Python Selenium: Login automation, Layer Problem?

So I am using Python Selenium to Login through a Webpage, i have done that before, my code was working pretty fine in that case:
driver.find_element_by_name("username").send_keys("user")
driver.find_element_by_name("password").send_keys("password")
driver.find_element_by_xpath("//input[#type='submit' and #value='Login / Register']").click()
But now, as I would like to do that again, it doesnt work anymore. I have tried some variations but none of them have worked for me, I also tried to access through layers, which I have seen in some older cases:
wait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.ID, "loginmask")))
driver.find_element_by_id("yourName").send_keys('username')
.....
I also tried something like that:
frame = driver.find_element_by_id("mainBody")
driver.switch_to.frame(frame)
driver.find_element_by_id("yourName").send_keys('username')
But somehow the find_element_by_id function isnt working for me, but the id is the only thing I can search for in that case.
enter image description here
this image of the login page might be helpful, as you can see the layers i might have to bypass. Maybe you can help me how to get the Element 'yourName'
Better Picture of HTML Body:
enter image description here
HTML Body:
<body id="mainBody" style="visibility: visible;">
<div id="treeLeft" style="height: 882px;">
<img id="logoLeft" src="img/ax_01.png" alt="">
<div id="buttonAreaLeft">
<img id="btnLogout" src="img/logoff.gif" title="logout" alt="" style="float:right">
<img id="btnHome" src="img/home.gif" title="startpage" alt="" style="float:left">
<br clear="all">
</div>
<div id="myMenuAccordion"></div>
</div>
<div id="mainLayer" style="left: 230px; width: 1125px; height: 860px; top: 12px;"><h1>AX Controller WEB-Interface</h1>
<div id="loginLayer" style="position:absolute;top:120px;left:120px;">
<h1>PLEASE LOGIN</h1>
<div class="hspacer5"></div>
<div class="hspacer5"></div>
<div id="loginmask" class="form">
<form method="post" action="index.php" onsubmit="venue.login();return false;">
<span class="label block">Your Name:</span><input type="text" id="yourName" maxlength="20" style="width:110px;" autocomplete="off"><br>
<div class="hspacer5"></div>
<span class="label block">Password:</span><input type="password" id="authCode" maxlength="20" style="width:110px;" autocomplete="off"><br>
<div class="hspacer5"></div>
<span class="label block"> </span><input type="submit" id="btnLogin" value="login" style="width:112px;padding:1px 0px;">
</form>
</div>
</div>

How to find context label using Python webdriver?

With this HTML code, I would like to extract the text ".Fermé(e)" :
<A onclick="return false;" onkeydown=campaignSPTree.onKeyDown(this); href="#r2">
<SPAN>
<LABEL class=hidden-label>Tree level 0, 3 of 3. </LABEL>
<SPAN id=r2c0Content name="r2c0Content">
<IMG border=0 alt="" src="/wfo/bbm/images/icons/campaign.gif?v=11.1.1.7673"> UAT Sud Est
</SPAN>
<LABEL id=campaignSPTreer2ChildInfo class=hidden-label childInfo=", node has 24 items. Press the right arrow key to show/hide this row's child items.">.Fermé(e)</LABEL>
</SPAN>
<LABEL class=hidden-label name="SelectInfo"></LABEL>
</A>
I thought I could do it using the following command, but it doesn't work :
infoOpen = browser.find_element_by_id("campaignSPTreer2ChildInfo").text
I don't have an error message but the value infoOpen is empty.
Could you please help me ?

Selenium WebDriver Exceptions - "element not interact-able"

I am trying to select elements from a drop down box, which loads options once clicked. I can get to the element but not interact with it. The error is NOT due to the page not fully loading as most related questions are.
I've tried selecting element by Id, Xpath, and using the js to make the element not hidden, none have worked so far. the latest i've tried was to send the keys.down to activate the list... still get the "not interactable" error.
Web page with selector--
</div>
</div>
<div class="css-1wy0on6 av__indicators">
<span class="css-bgvzuu-indicatorSeparator av__indicator-separator">
</span>
<div aria-hidden="true" class="css-1u02eyf-indicatorContainer av__indicator av__dropdown-indicator">
<svg aria-hidden="true" class="css-19bqh2r" focusable="false" height="20" viewbox="0 0 20 20" width="20">
<path d="M4.516 7.548c0.436-0.446 1.043-0.481 1.576 0l3.908 3.747 3.908-3.747c0.533-0.481 1.141-0.446 1.574 0 0.436 0.445 0.408 1.197 0 1.615-0.406 0.418-4.695 4.502-4.695 4.502-0.217 0.223-0.502 0.335-0.787 0.335s-0.57-0.112-0.789-0.335c0 0-4.287-4.084-4.695-4.502s-0.436-1.17 0-1.615z">
</path>
</svg>
<span class="sr-only">
Toggle Select Options
</span>
</div>
</div>
</div>
<input name="organization" type="hidden" value="" />
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-12 col-md-10 col-lg-8">
</div>
</div>
<div class="row">
<div class="col">
<button class="btn btn-primary disabled" disabled="" type="submit">
Continue
</button>
</div>
</div>
</form>
</div>
</div>
Some Python code used so far--
elem = driver.find_element_by_name("organization")
js = "arguments[0].style.height='auto';
arguments[0].style.visibility='visible';"
driver.execute_script(js, elem)
from selenium.webdriver.common.keys import Keys
elem.send_keys(Keys.DOWN)
###not interactable Error
I would expect the item to allow me to select or activate the options list at the least. I have been successful in lists, but not this new type.
The INPUT you are trying to interact with is of type="hidden" so it won't be visible and can't be interacted with using Selenium. My guess is that there's a dropdown that isn't a SELECT that is displayed, the user makes a selection, and then code pushes that value into the hidden INPUT. Just ignore the hidden INPUT and interact with the page as a user would by clicking the dropdown, then clicking your selection. The rest should take care of itself.
We can't make more recommendations on locators, etc. without more of the HTML of the page since you stated that the site is behind an account.

How I can send a message on Facebook using Selenium Driver in Python

I am trying to send a message to my FB friends using selenium in python. I've tried Element by Xpath, CSS Selector, Class but it doesn't work please help me how it would be I'm trying using
Message_button = browser.find_element_by_id("u_0_t")
Message_button.click()
time.sleep(10)
message = browser.find_element_by_xpath("//textarea[#class='luiTextareaAutogrow _552m']")
message.send_keys("Hi this is Mark how are you..!")
message.submit()
time.sleep(10)
Here is the FB Code
<div class="fbNubFlyoutFooter">
<div class="_552h _n4k">
<textarea style="height: 15px;" class="uiTextareaAutogrow _552m" data-ft="{"tn":"+N"}" onkeydown="run_with(this, ["legacy:control-textarea"], function() {TextAreaControl.getInstance(this)});"></textarea>
</div>
<div style="right: 0px;" class="_552n">
<div class="_10nr"></div>
<div class="_6a _552o">
<form action="https://upload.facebook.com/ajax/mercury/upload.php" class="_vzk" title="Add Photos" method="post" data-reactid=".12">
<input name="attach_id" data-reactid=".12.0" type="hidden">
<input name="images_only" value="true" data-reactid=".12.1" type="hidden">
<div class="_m _4q60 _3rzn _6a" data-reactid=".12.2"><a tabindex="0" class="_4q61 _5f0v _509v" data-reactid=".12.2.0"><i class="_509w" alt="Camera" data-reactid=".12.2.0.0"></i></a>
<input class="_n" name="attachment[]" multiple="" accept="image/*" title="Add Photos" data-reactid=".12.2.1" type="file">
</div>
</form>
</div>
<div class="uiToggle _1tn3 emoticonsPanel" data-ft="{"tn":"+O"}"><a class="_4vui" tabindex="0" title="Choose a sticker or emoticon" rel="toggle" role="button"><i class="_4l9x"></i></a>
<div class="_5r8f panelFlyout uiToggleFlyout">
<div class="_5r8p">
<div style="padding:30px;text-align:center;" data-reactid=".13"><span class="img _55ym _55yq _55yo _5d9-" aria-label="Loading..." aria-busy="true" data-reactid=".13.0"></span>
</div>
</div>
<div class="panelFlyoutArrow"></div>
</div>
</div>
<div class="lfloat"></div>
<div class="_5g2o"><a tabindex="0" aria-label="Send a Like" class="_3s0d" title="Send a Like" data-reactid=".10"><i class="_2y9i _30yy" data-reactid=".10.0"></i></a>
</div>
</div>
</div>
Please help me am I using the correct method
you can use css_selector and Keys.ENTER as follows:
# enter your text by using css selector
driver.find_element_by_css_selector(".uiTextareaAutogrow._552m").send_keys("Merhaba, nasilsiniz?")
# you can send your message by sending native keyboard ENTER
driver.find_element_by_css_selector(".uiTextareaAutogrow._552m").send_keys(Keys.ENTER)

Categories