Python+Selenium Scrape Error: ElementNotVisibleException - python

I'm currently trying to use Selenium/Python/PhantomJS to scrape the results of the form below:
http://gis.vgsi.com/newhavenct/Sales.aspx
It looks like when I try to click on the search button, I get an ElementNotVisibleException error.
My code:
self.driver.find_element_by_id("MainContent_btnSearch").click()
After some digging online, it seems like the button may be hidden. Indeed, here is the relevant HTML code from the search page:
<input type="button" value="Search!" class="btn btn-primary searchTrigger" style="width: 200px;" />
<input type="submit" name="ctl00$MainContent$btnSearch" value="Search" id="MainContent_btnSearch" style="width: 200px; display: none;" />
<div id="MainContent_ctl00" style="display:none;">
</div>
I tried preceding my previous code with a click of the searchTrigger, but this is still not working:
self.driver.find_element_by_class_name("searchTrigger").click()
self.driver.find_element_by_id("MainContent_btnSearch").click()
Any advice would be greatly appreciated!

Requested element has attribute style="display:none;", so you need to make it visible
Try to use following code:
self.driver.execute_script('document.getElementById("MainContent_btnSearch").style.display="block";')
self.driver.find_element_by_id("MainContent_btnSearch").click()

A portion of the screen is not visible by Selenium. It may be that another element lies on top of the one you are trying to click.
For me what always works without spending hours searching for the cause is to click using Javascript:
self.driver.execute_script("arguments[0].click();", elt)
where elt is a WebElement - e.g. returned by find_element_by_id(.).
Make it a function and use wherever the problem occurs.
Edit: it is a generic answer for when it happens again, but in this particular case the other answer is probably correct.

Related

Trouble clicking a three way toggle switch using selenium webdriver with Python

I want to click a three way toggle using selenium with python.The html element Ids are dynamic. So I have tried with an XPath where class contains a specific text! But I seeing 'element not found'/'element not visible' whole day!
I've tried with below line of code but no help.
browser.find_element_by_xpath("//*[contains(#class,'switch switch-three toggle ios') and contains(text(),'Available')]").click()
Here is the HTML code of the page and I want to click on - 'Available'
<label class="switch switch-three toggle ios" id="radio8526" onclick="" style="float: left; width: 300px; height:15px; margin-right: 20px;margin-left: 20px;">
<input id="available8526" value="available" onclick="setVersioningIdFun('8526');document.getElementById('toggleStateButton').click();;" name="onoffswitch8526" type="radio">
<label for="available8526" onclick="">Available</label>
<input id="unavailable8526" value="unavailable" onclick="setVersioningIdFun('8526');document.getElementById('toggleStateButton').click();;" name="onoffswitch8526" type="radio">
<label for="unavailable8526" onclick="">Unavailable</label>
<input id="archived8526" value="archived" onclick="setVersioningIdFun('8526');document.getElementById('toggleStateButton').click();;" name="onoffswitch8526" type="radio" checked="">
<label for="archived8526" onclick="">Finalised</label>
<a class="slide-button"></a>
</label>
From w3c documentation You can use this to solve your problem
browser.find_element_by_css_selector('input[id^="available"]').click()
You can just use the value attribute, e.g.
input[value='available']
You might need to add a wait for clickable to make sure the page has loaded. See this link for more info and some examples.

Selenium (python) text deletes on its own

Selenium is able to find these elements and send_text to them, but right after the text is written it is automatically deleted. Am I calling .send_text() wrong, or am I finding the wrong element to interact with?
This is a popup frame dialog, but I can't seem to focus on the frame. I'm not even sure I need to as selenium still interacts with the text fields.
Here is the javascript:
<div class="control-group">
<label class="control-label">Patient ID</label>
<div class="controls">
<input data-ng-model="subject.Code" data-custom-error="Patient ID already exists in this center" class="ng-valid ng-dirty" data-original-title="" type="text">
<i class="required-field" style="top:inherit">*</i>
</div>
</div>
Here is one method I have been using:
patientID = 'testID'
driver.find_element_by_xpath("//input[#data-ng-model='subject.Code']").send_keys(patientID)
also,
driver.find_element_by_css_selector(".controls > input[data-ng-model='subject.Code']").send_keys(patientID)
As I said, both find the correct element and send text but the text is immediately deleted. Any ideas, I'm stuck.
I'm using firefox, but the same happens in chrome.
Have you checked if there is any javascript code in the page listening to changes to the input field and deleting its contents?

Python and Selenium Search for Text Outside of Tags

I'm trying to make a selenium script using python that is able to search for a specific text and click on it. Below is an example of the code I'm working with. In this example I'm searching for the string "Next" and clicking on it.
<div class="btn-container text-center">
<button id="btnNext" class="btn btn-lg btn-primary btn-block">Next</button>
</div>
So far I have tried the following:
driver.find_element_by_xpath("//*[#id='btnNext']").click()
driver.find_element_by_id('btnNext').click()
driver.find_element_by_xpath("//*[contains(text(),'" + 'Next' + "')]").click()
driver.find_element_by_xpath("//button[contains(.,'Next')]").click()
But no luck so far. Any suggestions? Thank you for all the help!

Selecting form input with Selenium isn't working?

I was making an software which should connect to website using Selenium (firefox, python) and click "Yes" button. Here's page's source code (just that form part):
<div class="grid_24">
<div class="content_block">
<form action="" method="POST" class="fatform">
<p style="text-align:center;">
<input style="margin-right:50px;float:none;" type="submit" name="yes" value="da">
<input style="float:none;" type="submit" name="no" value="ne">
<input type="hidden" name="checkthis" value="lpftvqenq">
</p>
</form>
</div>
I was looking at Selenium Python Docs but I couldn't solve it by myself. So I'm here. I tried few types of selecting elements like this:
proxy(ip, port).find_element_by_name('yes').click()
form = proxy(ip, port).find_elements_by_class_name("fatform") #printing this gives nothing
proxy(ip, port).find_element_by_css_selector(".yes[value='da']").click()
All these examples (except form string which returns "") return NoSuchElementException.
Thanks in advance! :)
Why not search for the element by xpath instead? From my experience, the XPath generated by the Firebug add-in for Firefox works extremely well for me.
proxy(ip, port).find_element_by_xpath("xpath text").click()
You are using wrong selector.
. in css selector means class which is not the case here. Should look like
[name='yes'][value='da']
EDIT
Including explicit wait
WebDriverWait(proxy(ip, port), 10).until(
EC.find_element_by_css_selector((By.CSS_SELECTOR, "[name='yes'][value='da']"))
proxy(ip, port).find_element_by_css_selector("[name='yes'][value='da']").click()

Unable to upload image/file in python using selenium webdriver

I'm unable to upload and save an image.
The following is the piece of code that facilitates file upload using the UI. At first, only the upload button is visible and once an image is selected from the uploader the save button is visible.
<td colspan="1" class="actions">
<input type="button" class="button" name="logo" id="logo-f-upload" value="Upload Image"/>
<input type="file" id="logo-upload" accept="image/*" name="logo-upload" style="display:none" />
<input type="submit" value="click to save" name="submit_logo_now" class="main submit_dm logo-save-focused" style="display: none"/>
</br>
</td>
I tried driver.find_element_by_id("logo-f-upload").send_keys(os.getcwd()+"/image_test.png")
and also
driver.find_element_by_id("logo-upload").send_keys(os.getcwd()+"/image_test.png")
but it doesn't throw any error at this stage but on the next where it says "element is not visible ..." ie. the save button. On the UI, when simulating this, the file uploader doesn't open but the file_upload button value changes to the path of the image and then breaks.
I believe this is the answer which is merely a JS hack: So the problem is that the logo-upload input has style set to display:none and due to this, selenium can't find the element. The solution that works for me is using javascript to set the style to display:block, after which using send_keys(img_path) works fine.
dr.execute_script("document.getElementById('logo-upload').setAttribute('Style','display:block')")

Categories