Python Selenium: I can't click this exit button - python

The exact button I am trying to click is this:
https://www.bungol.ca/map/location/toronto/?
close the property slider on the left
click tool -> open list
click on any of the listing and you get to something like this:
Image for close button
Here's what I tried to do:
time.sleep(5) #wait for page to fully load
driver.find_element_by_xpath("""/html/body/div[17]/div/div/div/button""").click()
and
time.sleep(5) #wait for page to load
driver.find_element_by_css_selector("""html body#body.modal-open div#listingInfoModal.modal.fade.show div.modal-dialog.modal-xl div.modal-content div.modal-body button.close""").click()

This CSS selector will do it.
#listingInfoModal button.close

Related

Prevent/delay Modal Dialog Box from closing using Selenium in Python

I am trying to retrieve the page name of each page on a site. The steps are the following:
Click on the settings icon
a Modal Dialog Box appears (That is what I believe they are called?). The Dialog Box appears for three seconds before closing.
Click on "Rename Page"
A Popup- window appears with a text field (containing the page name). The popup also contains the buttons "Ok" and "Cancel
Retrieve the name in the text field
Press Ok
Go to the next page. Repeat stages 1-4
For some reason at stage 2, seemingly at random, I receive a TimeOutException error. I say at random, because the error occurs on sometimes on say, the 55th page and sometimes 130th page and so on. I believe it might be because the dialog box closes, before the Rename Page button has been clicked (Which is weird since 3 seconds should be ample time to click on the button.
Screenshot
I am using the framework Spyder to write code in. When the TimeOutException occurs, if I run the code from Step 1. Everything works again without error. So for some reason, sometimes the error occurs and sometimes not.
The HTML code for the Settings Icon is the following:
<div
id="_labsfluxbar_WAR_labsfluxbarportlet_showCurrentPageSettingsPopupButton"
class="labs-fluxbar-portlet-current-page-settings-popup">
</div>
The HTML code for 'Rename Page'
<ul
class="labs-fluxbar-portlet-page-menu-items">
<li class="labs-fluxbar-portlet-page-menu-item labs-fluxbar-portlet-page-menu-item-rename" title="Rename this page"
onclick="_labsfluxbar_WAR_labsfluxbarportlet_renamePage('9398653')"> Rename page
</li>
The code in Python:
i = 0
while True:
#Get page name
# Step 1 - Click on Settings icon
settings = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "_labsfluxbar_WAR_labsfluxbarportlet_showCurrentPageSettingsPopupButton"))) #Search for settings button
settings.click() #click settings icon
# Step 2 - Click on the "Rename Page" button
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='labs-fluxbar-portlet-current-page-settings-popup-content']//li[#title='Rename this page']"))).click() #Click on the Rename page element
# Step 3 - Retrieve page name from text field
page = driver.find_element_by_xpath("//input[#id='popup_prompt']")
page_name = page.get_property("value") #Copy the name of page
print(page_name)
# Step 4 - Click ok to close PopUp window
driver.find_element_by_xpath("//input[#id='popup_ok']").click() #Exit the popup window
# Step 5- Go to the next page
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "_labsfluxbar_WAR_labsfluxbarportlet_pageDropdownList")))
select = Select(driver.find_element_by_id('_labsfluxbar_WAR_labsfluxbarportlet_pageDropdownList'))
select.select_by_index(i)
i += 1
The TimeOutExcpetion (sometimes) occurs after running the following line, however I don't get why?
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='labs-fluxbar-portlet-current-page-settings-popup-content']//li[#title='Ange ett nytt namn för sidan']"))).click()
Does anyone have any suggestion what is occurring, or what could be done to prevent it?
Embarrassingly the issue was not due to the code.. It was because of a bad/slow internet connection.

Is there a way to block/close iframes using seleium?

I am creating a downloader from Instagram. This program gets URLs from top Instagram posts in a given hashtag and inputs them into a downloader. The issue is that a popup iframe ad always appears over the download button when the website is first loaded. This throws an error that the button cannot be clicked because the iframe will be clicked.
This is for Python Selenium running Chrome driver. I have tried to run a filter that finds iframes and goes back to the main page:
all_iframes = self.browser.find_elements_by_tag_name("iframe")
if len(all_iframes) > 0:
self.browser.switch_to.default_content()
This did not work, I also tried to get the XPath to the X-button on the ad, but the ID changes every time, so I cannot be clicked or identified.
#get the website
for link in self.links:
self.browser.get('https://downloadgram.com/')
time.sleep(5)
#this is the x button click iframe I tried, but the XPath changes
all_iframes = self.browser.find_elements_by_tag_name("iframe")
if len(all_iframes) > 0:
xButton = self.browser.find_element_by_xpath('//div[#id="id3019a64023cross3019a64023"]')
xButton.click()
#inputs the URL from array links[] into download box
input = self.browser.find_element_by_xpath('//input[#name="url"]')
input.clear()
input.send_keys(link)
time.sleep(1)
#clicks download button
download = self.browser.find_element_by_xpath("//input[#type='submit']")
download.click()
time.sleep(1)
#clicks confirm button
actuallyDownload = self.browser.find_element_by_xpath("//a[#target='_blank']")
actuallyDownload.click()
time.sleep(1)
I expect the code to download the pictures at the url, but I get:
selenium.common.exceptions.WebDriverException: Message: unknown error: Element <input type="submit" value="Download" class="button"> is not clickable at point (451, 446). Other element would receive the click:
the website (turn off adblock to see add) https://downloadgram.com/
I am able to close the pop up with following code.Please try that.
browser.get('https://downloadgram.com/')
time.sleep(5)
element=browser.find_element_by_xpath("//div[starts-with(#id,'id')]" and "//div[starts-with(#style,'position:absolute !important;height:20px !important;width:20px !important;top:3px !important;left:3px !important;background-image:url(data:image/png;')]")
arrt=element.get_attribute("id")
print(arrt)
browser.execute_script("arguments[0].click();", element)
Let me know if it works.Good Luck.

Getting out of popup form after downloading PDF selenium python chromedriver

I recently got help with downloading a PDF file from a popup form, but now I would like to exit the form and for some reason I can't perform any actions after downloading the PDF.
I've tried ActionChains(driver).send_keys(Keys.ESCAPE).perform() and it doesn't exit the form. I've also tried driver.back() but that goes back one page and I would like to stay on the current page to continue downloading files.
Here's the code so far:
options = webdriver.ChromeOptions()
prefs = {"download.default_directory":"C:/Users/gille/Documents/SJ Webscraping/data","plugins.plugins_list":[{"enabled":False,"name":"Chrome PDF Viewer"}]}
options.add_experimental_option("prefs",prefs)
driver = webdriver.Chrome(options=options)
driver.get("https://www.southtechhosting.com/SanJoseCity/CampaignDocsWebRetrieval/Search/SearchByElection.aspx")
driver.find_element_by_xpath('//*[#id="ctl00_DefaultContent_ASPxRoundPanel1_btnFindFilers_CD"]').click()
driver.find_element_by_xpath('//*[#id="ctl00_GridContent_gridFilers_DXCBtn0"]').click()
driver.find_elements_by_xpath('//td[#class="dxgvCommandColumn_Glass dxgv"]//img[#title="View Form"]')[0].click()
driver.switch_to.frame(driver.find_element_by_tag_name('iframe'))
a = driver.find_element_by_link_text("Click here")
ActionChains(driver).key_down(Keys.CONTROL).click(a).key_up(Keys.CONTROL).perform()
#insert exit sequence here
Trying to exit out of the following popup form:
I had updated my answer here, but incase you missed it,
Try this to close the popup:
driver.switch_to.default_content()
driver.find_element_by_xpath('//*[#id="ctl00_GenericPopupSizeable_InnerPopupControl_HCB-1"]/img').click()
Explanation: The close button of popup is not in the iframe where download link is seen, so we need to go back to main content and then click on popup close button.

Clicking a Button in a formbox with selenium WebDriver in python

I am new to python and selenium. I am trying to click a button in a formbox, but cannot click, only highlight the button. I am trying to click on the button named "Delivery System"
My code is:
delivery_system = browser.find_element_by_name("Delivery System")
browser.execute_script("arguments[0].click();", delivery_system)
delivery_system.click()
The html is:
HTML Code
When I try my code it just highlights the button, but doesn't click/follow it?
ANy ideas?
i would advice you change
delivery_system = browser.find_element_by_name("Delivery System")
browser.execute_script("arguments[0].click();", delivery_system)
delivery_system.click()
to
delivery_system = browser.find_element_by_name("Delivery System").click()
or select by classname
driver.find_element_by_css_selector('.browseLink').click()

Element is not clickable (the button is blocking by other element)

I'm trying to click on this button: browser.find_element_by_id('btnSearch')
But this button is blocking by this div tag: <div id="actionSearch" class="row pull-right">
How do I go around to click this button with id='btnSearch" while it's blocking by the actionSearch div?
I tried the following:
browser.find_element_by_id('btnSearch').click()
browser.implicitly_wait(10)
el = browser.find_element_by_xpath('//*[#id="btnSearch"]')
ActionChains(browser).move_to_element_with_offset(el, 1827, 270)
ActionChains(browser).click()
ActionChains(browser).perform()
element = browser.find_element_by_id('btnSearch')
browser.execute_script("arguments[0].click();", element)
wait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, //*[#id="btnSearch"]'))).click()
none of these work.
Can anyone help me with this? I've spent two days trying to click this button!! Please help!
Considering provided image of HTML source (tip: do not provide it as image, but as text instead) I can assume that required element located in the bottom of page and you might need to scroll page down to be able to handle it.
Try below code
link = browser.find_element_by_id("btnSearch")
browser.execute_script("arguments[0].scrollIntoView()", link)
link.click()
Note that link is not a pseudo-element (is not located inside ::before/::after pseudo-element), so it can not be the cause of your problem
As for your code:
ActionChains(browser).move_to_element_with_offset(el, 1827, 270)
ActionChains(browser).click()
ActionChains(browser).perform()
Here you're trying to make scroll to link with huge offset and then you make click on current mouse position - not on link
You can try to modify it as
ActionChains(browser).move_to_element(el)
ActionChains(browser).click(el) # Pass WebElement you want to click as argument to `click()`
ActionChains(browser).perform()

Categories

Resources