Issue trying to tweet an image/video using Selenium - python

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.

Related

Selenium can not move to next page

I try to crawl data from a dynamic web using selenium. It required an account to log in, and I must click on some link to forward to information page. After doing all these steps, I found that the source code is not changed and I can not get element that exist on new page. In otherhands, I get direct to this page, and do login but source code I get is parent pages. Can you explain to me why and how to tackle this problem?
how I perform click action
element = driver.find_element(By.CLASS_NAME, "class_name")
element2 = element.find_element(By.CSS_SELECTOR, "css_element")
element2.click()
how I get source code:
page_source = driver.execute_script("return document.body.outerHTML")
with open('a.html', 'w') as f:
f.write(page_source)

Selenium Automation "Unable to locate element" error on the web UI of qbittorrent application

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()

What is Google Image's id or class that corresponds to the box that lets you drag and drop images?

I am using Python Selenium to make some sort of Python Console version of Google Images. I already got the part where it opens up and clicks the camera icon. Unfortunately, I don't know what the id or class is for the box that lets you drag in images, as when I try to use an Id from what appears to be the box it says "Element not interactable"
the code so far:
from selenium import webdriver
import time
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://images.google.com")
print("Googlen't Images")
image_query = input("Enter path where image is: ")
cameraicon = driver.find_element_by_class_name("BwoPOe")
cameraicon.click()
time.sleep(2)
box = driver.find_element_by_id("dRSWfb") #this is the one that gives "element not interactable" error
box.send_keys(image_query)
Can anyone help?
First: error gives line with send_keys(), not find_... - so your comment in code is misleading.
Problem is that "dRSWfb is a <div> and you can't send keys to <div>. Inside this <div> is <input> which you should get and send keys.
This <input> has id Ycyxxc
box = driver.find_element_by_id("Ycyxxc")
box.send_keys(image_query)
I don't know how to drag'n'drop in Selenium (if it is even possible) but DevTools in Firefox shows events dragover and drop for <div> with id QDMvGf
EDIT: to send local file you can use button Browse on second tab
instead of drag'n'drop
which you can access using id awyMjb
box = driver.find_element_by_id("awyMjb")
box.send_keys(image_query)
Minimal working code
from selenium import webdriver
import time
print("Googlen't Images")
image_query = input("Enter path where image is: ")
driver = webdriver.Chrome("C:\Program Files (x86)\chromedriver.exe")
#driver = webdriver.Firefox(executable_path='/home/furas/bin/geckodriver')
driver.get("https://images.google.com")
cameraicon = driver.find_element_by_class_name("BwoPOe")
cameraicon.click()
time.sleep(1)
# send word or url on first tab
#box = driver.find_element_by_id("Ycyxxc")
#box.send_keys(image_query)
# send local file on second tab
box = driver.find_element_by_id("awyMjb")
box.send_keys(image_query)

Python Selenium Iframe issue on Bing

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()

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