Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot use click() in Selenium with mobile emulated chromedriver

I'm trying to test the login process of an internal web application using a mobile emulated chrome browser. I am using Python 3.6.4.

It finds the element by name, but won't click it!

Here is my code:

from selenium import webdriver
enter code herefrom selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
import time

mobile_emulation = {

    "deviceMetrics": { "width": 360, "height": 640, "pixelRatio": 3.0 },

    "userAgent": "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19" }

chrome_options = Options()
chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)
driver = webdriver.Chrome(chrome_options = chrome_options)

driver.get("https://internalweburlhere")
elem = driver.find_element_by_id("txtEmailAddress")
elem.clear()
elem.send_keys("Username")
elem = driver.find_element_by_id("txtPassword")
elem.send_keys("1234")
print("Password Entered")
driver.find_element_by_id("btnSignIn").click()
print("Logged In")

Here is the website source:

<div id="divSignIn" class="login-content-sign-in" style="display:block;">
<div id="divSignInHeader" class="login-content-header text-bold-10 label-colour" MandatoryField="False">Sign in using your Portal account</div>
<div id="divSignInMessage" class="login-content-sign-in-message success-label-colour text-bold-9" MandatoryField="False" style="DISPLAY:none;">Your account email address has now been verified, please enter your password and sign in</div>
<div id="upSignIn">
<div id="divSignInEmailAddress">
<div id="lblEmailAddress" class="entry-label">Enter your email address</div>
<div>
<input name="txtEmailAddress" type="email" id="txtEmailAddress" aria-autocomplete="none" autocomplete="off" maxlength="50" class="login-content-email-address entry-control entry-control-colour entry-control-border text-9" onkeyup="checkSignInButton()" />
</div>
</div>
<div id="divPassword">
<div id="lblPassword" class="entry-label" MandatoryField="False">Enter your password</div>
<div>
<input name="txtPassword" type="password" id="txtPassword" aria-autocomplete="none" autocomplete="off" class="login-content-password entry-control entry-control-colour entry-control-border text-9" onkeypress="checkCapsLockStatus(event,&#39;divSignInCapsLock&#39;)" onblur="hideCapsLockStatus(&#39;divSignInCapsLock&#39;)" onkeyup="checkSignInButton()" />
<div id="divSignInCapsLock" class="login-caps-lock-status">
<div id="divCapsLockDetail">
<img src="/Portal_Benchmark/Static/5.8.0.192/Images/dlg_icon_Exclamation.png" class="login-caps-lock-image" />
<span id="lblCapsLockMessage" class="login-caps-lock-message text-8" MandatoryField="False">Caps Lock Is On</span>
</div>
</div>
</div>
</div>
<div id="divSignInCaptcha" class="login-content-sign-in-captcha">
<div id="lblSignInCaptcha" class="entry-label">Enter the captcha text shown below </div>
<input type="hidden" id="objSignInCaptcha_clientState" name="objSignInCaptcha_clientState" /><div id="objSignInCaptcha" class="ig_Control igc_Control"><div class="igc_CaptchaImageArea"><img src="WebCaptchaImage.axd?guid=588a4174-70ca-4bf6-ac10-7ac77f74e6a4" title="" alt="" height="60" width="175" class="igc_CaptchaImage" /><input type="hidden" id="objSignInCaptcha__SignInEditor_clientState" name="objSignInCaptcha__SignInEditor_clientState" /><input title="{0}" id="objSignInCaptcha__SignInEditor" aria-autocomplete="none" autocomplete="off" onkeyup="checkSignInButton()" readonly="readonly" name="objSignInCaptcha__SignInEditor" maxlength="8" class="igc_CaptchaInput igte_Edit" type="text" style="width:175px;text-align:notset;" /></div><div class="igc_RefreshAndAudioButtonsArea"><a href="WebCaptchaImage.axd?guid=588a4174-70ca-4bf6-ac10-7ac77f74e6a4&amp;audio=true"><img alt="Listen to Captcha Audio" id="x:1812979435.0:mkr:AudioButton" src="Static/5.8.0.192/PortalStyleSheets/Infragistics/Default/images/igc_AudioButton.gif" /></a><a id="x:1812979435.1:mkr:RefreshButtonLink" href="#"><img alt="Refresh Captcha Image" id="x:1812979435.2:mkr:RefreshButton" src="Static/5.8.0.192/PortalStyleSheets/Infragistics/Default/images/igc_RefreshButton.gif" /></a></div><div style="clear:left;"></div></div>
</div>
<div class="login-content-sign-in-panel">
<div id="lblError" class="login-content-sign-in-error warning-label-colour text-bold-9">
</div>
<div class="login-content-sign-in-button">
<input name="btnSignIn" type="submit" id="btnSignIn" disabled="disabled" value="Sign In" />
</div>
</div>
</div>

Can anybody suggest how I might get around this? Interestingly, when I run similar code at BrowserStack on an android chrome browser, the clicks work as intended. Just not when I run it locally.

like image 800
CS10 Avatar asked Jan 10 '18 09:01

CS10


People also ask

What is WebDriver ChromeOptions ()?

ChromeOptions is a new concept added in Selenium WebDriver starting from Selenium version 3.6. 0 which is used for customizing the ChromeDriver session. By default when selenium opens up any browser (Chrome browser or Firefox browser), it opens up without any extension or history or cookies, etc.

How do I use Selenium to click a website?

We can find the button on the web page by using methods like find_element_by_class_name(), find_element_by_name(), find_element_by_id() etc, following which we can click on it by using the click() method. Code : Python3.


1 Answers

Some time element present in DOM but selenium failed to wait until the attribute get removed . Possible guess is In your case it is not waiting for remove attribute disabled="disabled" from your input tag and performing the click, even you can't see any exception.

So in such case you have to inject javascript in your browser and perform the action as given below :

element = driver.find_element_by_id("btnSignIn")
driver.execute_script('arguments[0].click();', element)
like image 135
NarendraR Avatar answered Sep 19 '22 22:09

NarendraR