Python Selenium Iframe issue on Bing - python

I am using selenium driver with python 3.6 and am looking to search for images of coffee cups and download the first image. However, I want the bigger image and the site url so I have selenium driver click on the image and open up the frame. When I try to download the image I get an error that no such element exists. I've tried the two scenarios below
driver.switch_to_frame('#OverlayIFrame')
driver.switch_to_frame(driver.find_element_by_selector('#OverlayIFrame'))
also tried pulling the first element in the frame to see whats there and nothing comes back. I am not sure what I am doing wrong but any help would be great.
bing_url = 'https://www.bing.com/'
driver = webdriver.Chrome()
driver.get(bing_url)
time.sleep(4)
# select the search box and enter a search condition
search_box = driver.find_element_by_xpath('//*[#id="sb_form_q"]')
search_box.send_keys('coffee cups', Keys.ENTER)
# click on the images tab
images_tab = driver.find_element_by_xpath('//*
[#id="b_header"]/nav/ul/li[2]/a')
images_tab.click()
select_first_image = driver.find_element_by_xpath('//*[#id="mmComponent_images_1"]/ul[1]/li[1]/div/div/a/div/img')
select_first_image.click()
# gives error that no such element exists
image_url = driver.find_element_by_xpath('//*[#id="mainImageWindow"]/div[1]/div/div/div/img')

Try this.If it works for you.
select_first_image = driver.find_element_by_xpath("//div[#class='cico br-pdMainImg']")
select_first_image.click()
image_url = driver.find_element_by_xpath("//div[#class='br-pdItemName br-standardText']").text
print(image_url)
driver.execute_script("window.open('{},_blank');".format(image_url))

Found the solution
driver.switch_to_active_element()

Related

selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable after switching the frame

I want to automate filling google forms with the images in google drive so I am using python with selenium.
The first image I can automate, but I got the error when I repeated the same process for the second image.
I created a sample google form to test.
https://forms.gle/fZ439KckkbpNU68v5
time.sleep(5)
driver.find_element_by_xpath('x path to first add file button').click()
frame= driver.find_element_by_class_name('picker-frame')
driver.switch_to.frame(frame)
*the first time it is working time*
driver.switch_to_default_content()
time.sleep(5)
driver.find_element_by_xpath('xpath to secondadd file button').click()
frame= driver.find_element_by_class_name('picker-frame')
driver.switch_to.frame(frame)
*after this, it causes an error*
I could not use the send_keys() method because this is button type.
Full code:
from selenium import webdriver
import random,time
option=webdriver.ChromeOptions()
driver = webdriver.Chrome("path to chrome driver")
driver.get("https://forms.gle/RvBYTZG5xuWoD8QA7")
driver.find_element_by_xpath('//*[#id="identifierId"]').send_keys("gmail")
driver.find_element_by_xpath('//*[#id="identifierNext"]/div/button').click()
time.sleep(3)
driver.find_element_by_xpath('//*[#id="password"]/div[1]/div/div[1]/input').send_keys("password")
driver.find_element_by_xpath('//*[#id="passwordNext"]/div/button').click()
time.sleep(5)
driver.find_element_by_xpath('//*[#id="mG61Hd"]/div[2]/div/div[2]/div[1]/div/div/div[2]/div/div[2]/span').click()
frame1 = driver.find_element_by_class_name('picker-frame')
driver.switch_to.frame(frame1)
driver.find_element_by_xpath('//*[#id=":6"]').click()
time.sleep(5)
driver.find_element_by_xpath('//*[#id=":1a.docs.0.1oe7tESa9uNnUC75FdYuqzPtPbLYz7QFi"]/div[2]').click()
driver.find_element_by_xpath('//*[#id="picker:ap:2"]').click()
driver.switch_to_default_content()
driver.find_element_by_xpath('//*[#id="mG61Hd"]/div[2]/div/div[2]/div[2]/div/div/div[2]/div/div[2]').click()
frame2 = driver.find_element_by_class_name('picker-frame')
driver.switch_to.frame(frame2)
driver.find_element_by_xpath('//*[#id=":6"]').click()
time.sleep(3)
This is a guess since no HTML was provided but maybe the location in the iframe of the element isnt correct since if you give it the global iframe path it want work i suggest to check if the button has a specifique i frame and switch to that iframe and give it the path in that one.
Or the element path isn't unique that makes problems too.
I think you are typing it wrongly. You should try this code.
driver.switch_to.default_content()
The solution is to put the refresh after switching the frame. Without having to use the switch_to_default_content() at all.
driver.refresh()
driver.switch_to.alert.accept()
It works.

Issue trying to tweet an image/video using Selenium

I'm trying to make a twitter bot using Selenium, Chromedriver, and Python. Currently I am able to login and reach my timeline but I am unable to figure out how to tweet an image/video. I am able to tweet text perfectly fine but am unable to do so for image/video.
My current code is:
driver.get(WEBSITE)
element = wait.until(EC.visibility_of_element_located((By.XPATH, '/html/body/div/div/div/div[2]/main/div/div/div/div/div/div[2]/div/div[2]/div[1]/div/div/div/div[2]/div[3]/div/div/div[1]/input')))
element.send_keys(os.getcwd() + "/downloads/image.jpg")
element = wait.until(EC.element_to_be_clickable((By.XPATH, '/html/body/div/div/div/div[2]/main/div/div/div/div/div/div[2]/div/div[2]/div[1]/div/div/div/div[2]/div[3]/div/div/div[2]/div[3]/div/span/span')))
element.click()
I first try to find the input XPATH and the second one is clicking the tweet button. The current error is that when I try to search for the element to upload a file it is unable to find it and times out. How can I get the image/video uploaded?
To upload an image or video you should send that image / video file path to the input file.
Let's say you want to upload an image located on you computer in the following path:
"C:\Users\you_name\Downloads\IMG_20180906_184135.jpg"
The input element to send this path is located on twitter home page by the following XPath "//input[#accept]", so to upload this file you can do the following:
driver.get(WEBSITE)
input_xpath = "//input[#accept]"
image_path = "C:\Users\you_name\Downloads\IMG_20180906_184135.jpg"
input_element = wait.until(EC.presence_of_element_located((By.XPATH, input_xpath)))
input_element.send_keys(image_path)
That's it, no need for any additional clicks etc.

can not switch to a new page using python selenium webdriver?

I need to scrape the CSV result of the website (https://requestmap.webperf.tools/). But I can’t.
This is the process I need to do:
1- load the website (https://requestmap.webperf.tools/)
2- enter a new website as an input (for example, https://stackoverflow.com/)
3- click submit
4- in the new page which opens after the submit, download the csv file at the end of the page
But I think the driver has the main page and doesn’t switch to the new page. That's why I can’t download the CSV file.
Would you please tell me how to do it?
here is my code:
options = webdriver.ChromeOptions()
options.add_argument('-headless')
options.add_argument('-no-sandbox')
options.add_argument('-disable-dev-shm-usage')
driver = webdriver.Chrome('chromedriver',options=options)
driver.get("https://requestmap.webperf.tools/")
driver.find_element_by_id("url").send_keys("https://stackoverflow.com/")
driver.find_element_by_id("submitter").click()
I have tested different ways to solve my issue:
First of all:
I got this code from here :
But it doesn't work.
window_after = driver.window_handles[1]
driver.switch_to.window(window_after)
I also tried this or this, but they are not working as well.
# wait to make sure there are two windows open
# it is not working
WebDriverWait(driver, 30).until(lambda d: len(d.window_handles) == 2)
# switch windows
# it is not working
driver.switch_to_window(driver.window_handles[1])
content = driver.page_source
soup = BeautifulSoup(content)
driver.find_elements_by_name("Download CSV")
So How can I get this issue solved?
Is there any other way in python to do so and switch to a new windows?

How to get iframe source from page_source when the id isn't on the iframe

Hello today i wanna ask how to get the link inside the page source but without id, i asked before how to get the link with id ok now i understand, but i've tried the same method with another link and i was not successful about that so here is my code:
from selenium import webdriver
# Create a new instance of the Firefox driver
driver_path = r"C:\Users\666\Desktop\New folder (8)\chromedriver.exe"
driver = webdriver.Chrome(driver_path)
# go to the google home page
driver.get("https://www.gledalica.com/sa-prevodom/iron-fist-s02e01-video_02cb355f8.html")
# find the element that's name attribute is q (the google search box)
element = driver.find_element_by_id("Playerholder")
frame = driver.find_element_by_tag_name("iframe")
driver.switch_to.frame("iframe")
link = frame.get_attribute("src")
driver.quit()
Like this here: enter image description here
There are multiple way to get it. In this case one of easiest is by using a CSS selector:
frame = find_element_by_css_selector('#Playerholder iframe')
This looks for the element with id = "Playerholder" in the html and then look for a child of it that is an iframe.

Selenium Python code

This is my very first time trying to scrape data from a website using Selenium. Fortunately I have got Selenium and Chrome to coordinate and the desired website opens.Once it opens up, I want to tell Python to click 'SEARCH' leaving the empty box blank (next to contains) and then tell Python to export the results ' and save the xlsx file as result_file. I do not know why the snippet is blowing up. Please provide your kind assistance.
from selenium import webdriver
driver = webdriver.Chrome("C:\Python27\Scripts\chromedriver.exe")
driver.get("https://etrakit.friscotexas.gov/Search/permit.aspx")
number_option = driver.find_element_by_id("cplMain_btnSearch")
number_option.click()
search_button = driver.find_element_by_id("cplMain_btnExportToExcel")
search_button.click()
result_file = open("result_file.xlsx", "w")
driver.close()
result_file.close()
Looking at the source of that page, the ID of the search button is "cplMain_btnSearch" not "SEARCH". And the Export button is "cplMain_btnExportToExcel".
To expand the answer of Daniel Roseman, you also need to specify the download location
options.add_argument("download.default_directory=C:/Python27")
driver = webdriver.Chrome(chrome_options=options)
The file will then be stored in your python27 directory with the name RadGridExport.csv.

Categories

Resources