Why can't I select this? - Selenium - python

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"))

Related

Upload file using Selenium opens windows file explorer [duplicate]

I was trying to automate a WordPress post content creation using Selenium Webdriver (Python). Unfortunately, I can not upload files in the post content. I have searched for the solution but most of them used send_keys which is not applicable for WP insert media (see image below). In the insert media, there are two options:
Select files
Drag files
I need a solution which will work for cross-platform (Windows, Linux etc).
I guess there might be a way using some jQuery, JS or something else. I am not very familiar with JS so I did not understand the solutions with JS.
P.S. I am working in Python, so a Python code will be helpful.
Here is the source for the media insert frame in case you need them:
<div class="media-frame-content">
<div class="uploader-inline">
<div class="uploader-inline-content no-upload-message">
<div class="upload-ui">
<h2 class="upload-instructions drop-instructions">Drop files anywhere to upload</h2>
<p class="upload-instructions drop-instructions">or</p>
Select Files
</div>
<div class="upload-inline-status">
<div class="media-uploader-status" style="display: none;">
<h2>Uploading</h2>
<button type="button" class="button-link upload-dismiss-errors"><span class="screen-reader-text">Dismiss Errors</span>
</button>
<div class="media-progress-bar">
<div></div>
</div>
<div class="upload-details">
<span class="upload-count">
<span class="upload-index"></span> / <span class="upload-total"></span>
</span>
<span class="upload-detail-separator">–</span>
<span class="upload-filename"></span>
</div>
<div class="upload-errors"></div>
</div>
</div>
<div class="post-upload-ui">
<p class="max-upload-size">Maximum upload file size: 32 MB.</p>
</div>
</div>
</div>
</div>
Finally, I've got a solution for WP add media file upload.
Actually, I have found that when the file selection window opens upon clicking the select files button, it generates a dynamic input field. Dynamic means, the ID for the input field is unique each time. Fortunately, the first part of the input field's ID remains same. For example, the ID is like html5_1bc7564i41pq5f7m1voce561a0e5. See the HTML below:
<input id="html5_1bc7564i41pq5f7m1voce561a0e5" style="font-size: 999px; opacity: 0; position: absolute; top: 0px; left: 0px; width: 100%; height: 100%;" multiple="" accept="" type="file"/>
So, what I did is, create an XPATH using the first part of the ID (html5_) and simply used send_keys and it worked perfectly. My XPATH is like:
input_file = "//input[starts-with(#id,'html5_')]"
The selenium code is:
driver.find_element_by_xpath(input_file).send_keys(file_path)
Here, file_path is the location of the file which I need to upload.
I missed the input field at the first place because it was not visible and did not realize that it is associated with the file upload. So I record the file upload steps with selenium IDE and found the dynamic input ID.
Thanks everyone, for all the suggestions and guidance.
drag and drop is possible:
Python Selenium WebDriver drag-and-drop
you can try move mouse and click "manually":
http://selenium-python.readthedocs.io/api.html#selenium.webdriver.common.action_chains.ActionChains.move_to_element
Here is the solution for you using AutoIT.
WordPress provides a select button to Upload New Media. So let us take help of that functionality.
On clicking "Select Files", File Upload dialog box would open up.
Pass the properties of File Upload dialog box to AutoIT exe.
AutoIT will fill up the filepath area with the selected file.
Now let the WebDriver click on the "Upload" button.
Let me know if this answers your query.

Selenium Can't find "HOME" element on Facebook

I'm doing some exercises with selenium, trying to scrape a few pages on facebook.
However, I'm not able to find the "Home" button link element to keep going. This on the left menu in any user's profile.
From what I'm seeing in the page code, the link is here:
<div class="_2yaa" data-key="tab_home">
<a class="_2yau" data-endpoint="/seudogshow/?ref=page_internal" href="/seudogshow/?ref=page_internal">
<span class="_2yav">Home</span>
<span class="img _55ym _55yn _55yo _2wwb" aria-busy="true" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuetext="Loading..."></span>
</a>
</div>
How would you guys go about clicking this button?
I tried with something like this:
driver.find_element_by_xpath("//a[contains(text(), 'Home')]").click()
or
wait.until(EC.element_to_be_clickable((By.XPATH, "//a[contains(text(), 'Home')]"))).click()
But I'm clearly doing it wrong.
The element you are trying to click has a span tag, not a. Try the following:
driver.find_element_by_xpath("//span[contains(text(), 'Home')]").click()

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

ElementNotVisibleException: element not visible Python

I'm new to Python and Selenium and I've come across an issue when trying to click on a button via webdriver.
The HTML of the div I'm trying to click through is:
<div class="">
<form method="POST">
<input class="hide" id="accept" name="accept" type="text" value="yes" readonly="">
<a href="/" class="btn btn-red">
<div class="svg-group group icn-bg-circle" data-png-fallback="">
<svg width="8" height="8">
<use xmlns:xlink=" " xlink:href=""></use>
</svg>
</div>
Decline
</a>
<button class="btn btn-green" type="submit">
<div class="svg-group group icn-bg-circle" data-png-fallback="">
<svg width="8" height="8">
<use xmlns:xlink="" xlink:href=""></use>
</svg>
</div>
Accept
</button>
</form>
</div>
I want to use .click() on the button with class name 'class="btn btn-green"'.
I have used the following code to select the element (after following the solutions in other similar SO questions.
driver.find_element_by_css_selector(".btn-green")
It looks like webdriver can find the element but when I try to apply .click() I get:
ElementNotVisibleException: element not visible
I then did some digging into the element (thinking I could bypass by using x, y coordinates to click through) but after using .size and .location I get:
{'x': 0, 'y': 0}
{'height': 0, 'width': 0}
Any help you could give on how to get around this would be really appreciated.
Thanks
The error hints to the potential problem, selenium does find it but it is not visible so you cannot click on it. You could check for visibility with is_displayed before clicking on it. Another possibility is if there is another button with that class that is hidden, try to see if you can select a visible element in the browser console (Control+Shift+J) with $(".btn-green")
Some xpaths that might work that are more robust for your problem:
//button[#type="submit" and text()="Accept"]
//button[#type="submit" and contains(#class, "btn-green")]
To click on the button with text as Accept you can use the following line of code :
driver.find_element_by_css_selector("button.btn.btn-green[type='submit']")

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

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.

Categories

Resources