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.
Related
I am working on 3 webpages (Let say Page A to C) with Python Selenium.
Both Page A and B contain this button:
<button id="continue-button" type="button" class="form-button" data-autom="button-label"><span>Click</span></button>
When clicking this button on Page A, it will jump to Page B; When clicking it on Page B, it will jump to Page C
I use the following code to perform auto click action on Page A, it works great.
element_confirm = driver.find_element("id", 'continue-button')
driver.implicitly_wait(5)
driver.execute_script("arguments[0].click();", element_confirm)
However, when I execute above code (different element name) again on Page B, nothing is happened.
No error message is on the log. I tried to print some text after driver.execute_script. It shows normally. I tried to find the element with "xpath", '//*[#data-autom="button-label"]', also no clcik action is performed. Checked the element is True (It means it can be found).
100% confirm that Page A to C are working fine when I click the button manually (using my mouse). Any recommendation?
Thank you!
Update:
As requested, this is the code that I use for Page B:
element_submit_two = driver.find_element(By.ID, 'continue-button')
driver.implicitly_wait(5)
driver.execute_script("arguments[0].click();", element_submit_two)
Aren't you using outdated selenium? According to docs, you should use like that:
from selenium.webdriver.common.by import By
button = driver.find_element(By.ID, "continue-button")
button.click()
I am trying to automate downloading of movies with magnet links using the BitTorrent web UI. I can click on the 'add torrent link' button and the popup does appear but after that, the code fails as it is unable to find the element where the torrent link needs to be added. The same problem occurs when I try to input the file location. I tried time.sleep but had no luck.
My code snippet:
def torrent(path, n):
#link to web UI
driver.get("http://127.0.0.1:8080/")
#default login credentials
username = driver.find_element_by_xpath("//*[#id='username']")
username.send_keys("admin")
password = driver.find_element_by_xpath("//*[#id='password']")
password.send_keys("adminadmin")
time.sleep(2)
driver.find_element_by_xpath("//*[#id='login']").click()
time.sleep(2)
#the elements I am trying to find
driver.find_element_by_xpath("/html/body/div[1]/div[1]/div[2]/a[1]").click()
time.sleep(5)
location = driver.find_element_by_xpath("/html/body/form/div/fieldset/table/tbody/tr[2]/td[2]")
location.send_keys(path)
input = driver.find_element_by_xpath("/html/body/form/div/textarea")
i = 0
while i <= n-1:
input.send_keys(list_torrent[i])
If you need any other information please let me know. I tried using BitTorrent API already but with no luck. The HTML page has a hidden overflow but it shouldn't be a problem since I am clicking on the elements which should make my code visible.
Thanks in advance.
Looks like it is in iframe.
switch to iframe like this :
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH," Ifrmae xpath here")))
and then do the other stuff here :
once done with iframe switch it back to
default_content like this :
driver.switch_to.default_content()
I am trying to scrape user reviews from the IMDB website using selenium. To load more reviews, I instantiated a click event which clicks the "load more" button on the review page . The issue is that the button gets clicked on the review pages of some movies but it does not work on review pages of other movies.
To give an example, the code given below works when the button is to be clicked on the "Iron Man" user review webpage but on the webpage of "Armageddon", it gives the error: NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".ipl-load-more__button"}
I find this a bit odd because the web element is the same and so if it works on one page, it should work for all the pages. However, I am not able to resolve this issue.
Here is the code which instantiates the click event:
def extend_page(wd): # wd is the webdriver
count = 20
while(count>0):
button = wd.find_element_by_class_name('ipl-load-more__button')
button.click()
wd.execute_script('window.scrollTo(0,document.body.scrollHeight);')
time.sleep(3)
new_height = wd.execute_script('return document.body.scrollHeight')
prev_height = new_height
count-=1
page = wd.page_source
return page
I have tried different methods for instantiating the click event such as find_element_by_name and find_element_by_xpath. I also tried both implicit and explicit wait to give some time for the webpage to load but to no avail.
Can anyone please tell me what could be the reason for this?
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.
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()