Double click works in Firefox but not chrome (Python/Selenium) - python

I have an element that requires a double_click().perform() action. It works great in Firefox, but doesn't in Chrome. A single click displays a menu, but a double click takes you to a directory. Here is my python/selenium step:
(webdriver.ActionChains(self.browser)).double_click(self.browser.find_element_by_id('nav-link-shopall')).perform()
The error message returns that the text I'm wanting to verify exists does not exist. Which is true if the double_click().perform() doesn't do its job.
I'm using Python 2.7.8 and Selenium 2.45.0.
I'm using Amazon.com as an example because the behavior is the same as the proprietary code I am testing. So here is the HTML code I'm attempting to click:
<div class="nav-left">
<div id="nav-shop">
<a href="/gp/site-directory/ref=nav_shopall_btn" class="nav-a nav-a-2" data-nav-tabindex="15" id="nav-link-shopall" tabindex="1">
<span class="nav-line-1">Shop by</span>
<span class="nav-line-2">"Department"
<span class="nav-icon nav-arrow" style="visibility:visible;"></span>
</span>
</a>
</div>
</div>

Try this:
variable = self.browser.find_element_by_id('nav-link-shopall')
actions = ActionChains(driver)
actions.move_to_element(variable)
actions.double_click(variable)
actions.perform()
You should use move to element.

Related

Selenium [python] not clicking an <li> button

So I have been trying to use selenium to click an <li> button on an HTML which is in a <ul> tag and looks something like this:
...
<ul id="ulVisualization">
<li class="active" id="liMap">Map</li>
<li id="liBar" class="">Bar</li>
<li id="liLine">Line</li>
</ul>
...
I have been using the following command to get to the element using XPATH:
WebDriverWait(driver, 50).until(EC.presence_of_element_located((By.XPATH,"//li[#id='liBar']"))).click()
Yet for some reason that I have not been able to identify, this command is not able to find/click that button and the command hits its time-out.
I have even tried:
driver.find_element_by_xpath('//li[#id="liBar"]').click()
But that too was of no avail, throwing an error message saying NoSuchElementException: Message:
I would appreciate any and all help and thank you very much in advance.
EDIT:
Additionally, I have noticed that when I click on the button manually the HTML modifies to:
...
<ul id="ulVisualization">
<li class="" id="liMap">Map</li>
<li id="liBar" class="Active">Bar</li>
<li id="liLine">Line</li>
</ul>
...
Revealing the data I am trying to acquire later in the HTML code, which was not available before I clicked the button manually.
Since you want to click on the Element, try element_to_be_clickable, instead of presence_of_element_located
WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.XPATH,"//li[#id='liBar']"))).click()
Make sure the locator you are using is Unique, that is 1/1. Link to Refer
And also check if the Elements are in an iframe or in an shadow-root or Trying to find Element in a newly opened browser tab.
If none of them work, apply some time.sleep() and check if it works.

Web element not found but appears when inspected manually

I am unable to access this menu in selenium the web element doesn't appear in inspector until manually done
<a id="cke_3275" class="cke_menubutton cke_menubutton__table cke_menubutton_off cke_menubutton__table" href="javascript:void('Table Properties')" title="Table Properties" tabindex="-1" _cke_focus="1" hidefocus="true" role="menuitem" aria-haspopup="false" aria-disabled="false" onmouseover="CKEDITOR.tools.callFunction(666,5);" onmouseout="CKEDITOR.tools.callFunction(667,5);" onclick="CKEDITOR.tools.callFunction(668,5); return false;">
<span class="cke_menubutton_inner">
<span class="cke_menubutton_icon">
<span class="cke_button_icon cke_button__table_icon" style="background-image:url(https://lms.testbook.com/vendor/ckeditor/plugins/icons.png?t=E6FD);background-position:0 -1896px;background-size:auto;">
</span>
</span>
<span class="cke_menubutton_label">
Table Properties
</span>
</span>
</a>
I tried accessing parent, click and actions.perform() nothing seems to work.
When i hover over the menu contents i see javascript:void('contentname'), i pasted this in the inspector and found the web element.
iframe=WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "/html/body/iframe")))
driver.switch_to.frame(iframe)
driver.find_element_by_tagname("a")
switch to iframe and then add rest of the code
driver.switch_to.default_content()
you need to switch to default content after you are done with the element and want to interact with an element outside the iframe

Why can't I select this? - Selenium

The following thing is embedded into a webpage. It is not part of the actual site:
What I've been trying to do is select the 'previous meetings' tab, using Selenium in Python.
These are the elements for both buttons (upcoming meetings and previous):
<div role="tab" aria-disabled="false" aria-selected="true" class="ant-tabs-tab-active ant-tabs-tab" tabindex="0" aria-label="Upcoming Meetings">Upcoming Meetings</div>
::before
Upcoming Meetings
</div>
<div role="tab" aria-disabled="false" aria-selected="false" class=" ant-tabs-tab" aria-label="Previous Meetings">Previous Meetings</div>
::before
Previous Meetings
</div>
I've tried driver.find_element_by_xpath('//*[#class=" ant-tabs-tab"]').click() but that didn't detect the element.
When I view the actual source code of the webpage, these Zoom elements didn't show up, so I think that's why Selenium doesn't recognise it either.
It doesn't have an ID or anything else, so I can't try them. Is there anything I can do? (e.g. Just click a location on the screen)
Or am I doing something wrong?
It seems to be an iframe, starting with
<iframe src="about:blank" name="tool_content" id="tool_content" class="tool_launch" allowfullscreen="allowfullscreen" webkitallowfullscreen="true" mozallowfullscreen="true" tabindex="0" title="Tool Content" style="height:100%;width:100%;" allow="geolocation *; microphone *; camera *; midi *; encrypted-media *; autoplay *" data-lti-launch="true"></iframe>
If that helps.
Update:
I tried using an extension for Firefox that gave you the exact xpath of an element:
driver.find_element_by_xpath('/html/body/div[1]/div/div/div[2]/div/div/div[1]/div[1]/div/div/div/div[1]/div[2]').click()
And this still didn't work - how come??
Due to having an iframe just switch to it
driver.switch_to.frame("tool_content")
Try using
driver.switch_to.frame(driver.find_element_by_class_name("tool_launch"))

Selenium right clicks at (0,0) instead of element but only after first loop

Note: Using chrome 76, a new tab opens for every file
I have to download a lot of files, and I have to right-click and save link as. This works for the first file, but for subsequent files it right clicks at (0,0).
The download link only appears after clicking a download button. I have tried using implicit and explicit waits, however, this doesn't seem to be the problem as the first file works fine. I have used the element.location property to check the location right before the click however it is the same whether it clicks in the right spot or not.
Python
downloadButton.click()
#have tried time.sleep here doesn't work
downloadLink = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, 'somepath'))
)
print(str(downloadLink.location)) #shows same correct location every time
actionChains.context_click(downloadLink).perform()
time.sleep(.1)
for i in range(4):
keyboard.send('down')
keyboard.send('enter')
time.sleep(1)
keyboard.send('enter')
Some HTML after the dl button has been clicked, can provide more if necessary
<div class="tab-content transparent no-padding-top margin-top">
<div class="tab-pane active" id="file">
<div data-bind="with: resource" class="form-horizontal form-panel">
<div class="form-group" data-bind="slideVisible: downloadedUrl() != ''" style="display: block;">
<label class="col-sm-3 control-label">Download Link</label>
<div class="col-sm-9">
<div class="bg-info padding">
<h4>Download version: <span data-bind="text: downloadName">Current Version</span></h4>
Click the link below to download this file.
<b>NOTE:</b> for larger files it may take a short time for the download to start.
<br>
<br>
<a style="word-break: break-all;" target="_blank" data-bind="attr: { href: downloadedUrl }, text: downloadedUrl" href="link">link</a>
</div>
</div>
</div>
hopefully that is somewhat formatted I copy-pasted from chrome console
Expected to right-click on the link element, clicks in the top left corner (0,0) instead

Error trying to login to webpage using selenium with python

I get an element isn't visible error:
ElementNotVisibleException: Message: u'Element is not currently visible and so may
not be interacted with'
For every find element line when I run this code:
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://www.example.com')
browser.find_element_by_name('username').send_keys('myusername')
browser.find_element_by_name('password').send_keys('mypassword')
browser.find_element_by_class_name('welcomeLoginButton').click()
The HTML for the login section of the page looks like this:
<div class='welcomeLoginUsername'>
<div class='welcomeLoginUsernameLabel'><b>Username:</b></div>
<div class='welcomeLoginUsernameInput'><input type='text' name='username' tabindex='1'>
<br><a class='sf' href='javascript: void(0);' onclick='showUsernamePopup();'>
<b>Forgot Username?</b></a>
</div>
</div>
<div class='welcomeLoginPassword'>
<div class='welcomeLoginPasswordLabel'>
<b>Password:</b>
<br><span class='sf'>(It's cAsE sEnSitIvE!)</span>
</div>
<div class='welcomeLoginPasswordInput'>
<input type='password' name='password' tabindex='2'>
<br><a class='sf' href="javascript: void(0);" onclick="showPasswordPopup();">
<b>Forgot Password?</b></a>
</div>
</div>
</div>
<input type="submit" value="" class='welcomeLoginButton' style='border: 0px;
padding: 0px; margin: 0px;) no-repeat;' onclick='document.forms["login"].submit()'>
Selenium interacts with the web browser in a similar way that the user would. So if there is an html element you're trying to interact with that is not visible then the simplest explanation is that when youre writing your selenium code you're not interacting with the web page like a normal user would.
In the end this isn't about the html of your web page its about the DOM and an element's hidden attribute. I suggest you download firebug or some other html viewer program, and then highlight the button you want to press. Use the DOM lookup for the html viewer and go through the sign in process manually. Notice what you have to do to make the element visible in order to interact with it then mimic the same steps in your selenium code.
If it is a matter of the fact that you did everything you needed to do, but selenium is interacting with the web page faster than the javascript will make the element visible then there is a wait that you have to programmed in.
Naive way:
import time
time.sleep(1) # this is done in seconds
More scalable manner:
import time
welcome_button = browser.find_element_by_class_name('welcomeLoginButton')
wait_for_element_visibility(welcome_button).click()
def wait_for_element_visibility(element):
if element.is_visible():
return element
else:
for i in range(10):
if not element.is_visible():
time.sleep(.5)
else:
return element

Categories

Resources