Handling Modal Dialogs with Selenium in Python - python

I am using Selenium to automate a process and the page I want to interact to opens a modal dialog every time someone access it. I want to be able to interact with the main page properly, so I need a way to close the dialog. However, I've tried some suggestions on handling modal dialogs and they are not working as they should. There are two buttons I can click on to close the dialog, one of them is:
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss="modal">Fechar</button>
</div>
I've tried to locate this button and then click on it with:
test = driver.find_element_by_link_text("Fechar")
test.click()
But this is not closing the dialog. Using:
test = driver.find_element_by_link_text("Fechar")
test.send_keys(Keys.RETURN)
gives me the following error:
no such element: Unable to locate element: {"method":"link text","selector":"Fechar"}.
I've also thought of writing a script to navigate through the dialog using the TAB key and then hitting Enter when the close button is reached. But I don't know if this the proper way to handle the problem and if this can be done without issues. Thanks in advance.

With find_element_by_link_text method, you won't find a button, you will find a link element (a). Reference.
If you want to get that button, you could use:
driver.find_element_by_css_selector('.modal-footer > button[data-dismiss="modal"]')

Related

I want to download F&O BhavCopy from the below link using Selenium Python

https://www1.nseindia.com/products/content/derivatives/equities/archieve_fo.htm
Here after entering the required details, I have tried many times to click on "GetData" button, but Selenium doesn't click on the button. I have tried Below lines of code to click on button but still got no luck.
driver.find_element(By.CSS_SELECTOR,"input[src='/common/images/btn-get-data.gif'][type='image']").click()
driver.find_element(By.XPATH,"//input[#src='/common/images/btn-get-data.gif' and #type='image']").click()
driver.find_element(By.CLASS_NAME,"getdata-button").click()
Here is the element from which I am trying to click on the button,
<input type="image" class="getdata-button" src="/common/images/btn-get-data.gif" onclick="validateInput()">
I tried to create a small script for that and I see what you mean. Nothing happens after the click.
I analyzed the behavior and I see that the click happens but for some reason all the requests hang in Pending status.
So, after the click, a request https://www1.nseindia.com/ArchieveSearch?h_filetype=fobhavzip&date=08-11-2022&section=FO is sent but it doesn't return any data. Nothing wrong with your code but I think they somehow detect a "bot" and stop processing all requests from this browser session. You cannot even refresh the page.

Selenium in Python: ElementNotInteractableException: Message: element not interactable

I know there are lots of posts about this exception, and I've read lots of them and tried their suggestions but they don't seem to work. Maybe you guys can see what I'm doing wrong.
I'm trying to scrape this page: https://www.kichink.com/stores/barshop
Specifically the information inside the little popup that apppears when clicking the "i" button next to the green "SEGUIR" button.
The HTML for that button is this:
<button id="about" class="btn btn-default btn-info-store" data-toggle="popover" data-original-title="" title=""></button>
I've tried many things to click it, but I just keep getting the element not interactable exception.
My last attempt was with:
element_boton = wd.find_element_by_class_name("btn.btn-default.btn-info-store")
element_boton.click()
The button seems to be correctly found, but I just can't click it.
Any suggestions?
Well, I have no idea why (so maybe someone can shed some light) but after using the full xpath (for some reason not even the relative xpath worked, it had to be the full one) to search for the button, it just started working, and i was able to click it.
element_boton = wd.find_element_by_xpath('/html/body/div[1]/div[3]/div/header/div/nav[2]/div/div['
'2]/div/ul[2]/li[1]/button')

Python Selenium - 2 buttons with same name

I am trying to click on a button but I am not able to do it because there are 2 buttons with similar name class, and I can't click the button I want.
Button 1:
<button class="dropdown-trigger">
<i class="icon2-arrow-down">
::before
Button 2:
<button class="dropdown-trigger is visible-desktop">
<i class="icon2-arrow-down arrow-icon">
::before
I want to click only in button 1, can you help me?
If you're using Chrome browser, (I don't know about other browser's developer's tool, but there must be something similar...) it's easy to pick up unique element.
Press 'F12' key to open Chrome developer's tool. (The page might be refreshed.)
Right click the button you want to focus on.
Click 'Inspect'.
In the developer's tool on the left side of your browser, something will be changed, and there should be a grey box across the HTML source code. (If you move your mouse on it, the button you've inspected should be highlighted)
Right click the grey-colored line then click 'Copy - Copy Selector'. (In your flavor, you can copy other features to find that element)
You can now use the copied selector (or any feature) to find exactly one element you've designated.
If you have any question, please leave comment on this post. thanks!
What happens if you try
driver.find_element_by_css_selector(".dropdown-trigger.visible-desktop").click()
Or you may try getting the element of the two it finds that is displayed...
buttons = driver.find_elements_by_class_name('dropdown-trigger')
button = next(filter(lambda x: x.is_displayed() == True, buttons))
button.click()

Can't click on webpage element using python selenium when used with mobile user-agent

I am trying to automate clicking on the play button of an embedded iframe music player on a webpage, I am coding using python 3 and Selenium to select the element to click.
When the user-agent is changed to replicate a mobile device, a wrapper layer then obscures the button that I can normally click, it asks if I want to listen on the main site or in the browser. I want to listen in the browser, but so far everything I have tried to click the correct element to achieve that, has failed.
I believe I am clicking on the wrong part but cannot find a way to access the correct part.
The inspect window of the browser shows the below for the part of the wrapper I need to click on:
<a class="mobilePrestitial__link sc-font-light" href="#">
Listen in browser
</a>
::before
"
Listen in browser
"
/a>
When I hover over the ::before part it highlights the part in the browser that I believe I need to click, but using right-click to inspect ::before just says 'scroll in view' so I cannot copy whatever the element represents.
My current code is this:
driver.switch_to.default_content()
driver.switch_to.frame(driver.find_element_by_partial_link_text('Listen in browser'))
element = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, '//a[#class="mobilePrestitial__link"]'))
)
element.click()
But it errors without a message, I suspect I need to be clicking on the element presented by ::before but cannot figure how to do that.
I found a workaround and solved this by hiding the wrapper overlay using Selenium in python script to make a Javascript call
driver.execute_script('document.getElementsByClassName("mobilePrestitial")[0].style.display=\"none\"')
once the overlay is hidden with the above code, the original button is accessible in the browser

How to click by robot framework if button is having only class

I need to click the button Calculate on the next webpage
https://www.seb.lv/eng/loan-and-leasing/leasing/leasing-calculator
Button is having next code, as you could see it does not have id or name
<button type="submit" class="btn btn-dark">Calculate</button>
Possibly, I have tried all different constructions according to the Robot framework tutorial, but still without any luck
Also I have tried to emulate the click with Javascript expression (which should be supported by Robot Framework)
Execute Javascript function getElementByXpath(path) {return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;};
getElementByXpath("//*[#class='btn btn-dark']").click();
Still without any success, although this construction works in the browser console.
Also: had tried this constructions in Firefox and Google Chrome Browsers as from some answers seen from here I was expecting that it could be some certain browser problem.
This button is on a iframe. you should switch to the iframe before trying to click.
driver.switch_to_frame(driver.find_element_by_xpath("//*[#class='calculator-frame']"))
driver.find_element_by_xpath("//*[#class='btn btn-dark']").click()
select frame (SeleniumLibrary) should help:
select frame css=.calculator-frame
click button css=.btn-dark

Categories