My selenium program isn't able to click on an input box - python-3.x

I have made a program to grab a youtube link and type it on a website but its not able to find the input box...plz tell me why
import time
from selenium import webdriver
import pyautogui as auto
url = 'https://www.youtube.com/results?search_query=closer+8d'
web = webdriver.Chrome('chromedriver.exe')
web.get(url)
time.sleep(3)
link = web.find_element_by_id('video-title')
link = link.get_attribute('href')
auto.hotkey('ctrl', 't')
auto.typewrite('https://www.youtubemp3.pro/en2')
auto.press('enter')
time.sleep(3)
search_bar = web.find_element_by_xpath('//*[#id="videoURL"]')
search_bar.click()
search_bar.send_keys(link)
The error I'm getting is:
Traceback (most recent call last):
File "e:\pranil\python\doing shit with python\songs downloader.py", line 17, in <module>
search_bar = web.find_element_by_xpath('//*[#id="videoURL"]')
File "C:\Users\Pranil.DESKTOP-TLQKP4G.000\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\Users\Pranil.DESKTOP-TLQKP4G.000\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
'value': value})['value']
File "C:\Users\Pranil.DESKTOP-TLQKP4G.000\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\Pranil.DESKTOP-TLQKP4G.000\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="videoURL"]"}
(Session info: chrome=83.0.4103.116)

The xpath you provided to find an element doesn't seem to work ""//[#id="videoURL"]"" I also opened up a YouTube video to see what you were looking for but I couldn't find videoURL if you are trying to get the URL I recommend you have selenium click on the share button and grab the link from there.

import time
from selenium import webdriver
import pyautogui as auto
url = 'https://www.youtube.com/results?search_query=closer+8d'
web = webdriver.Chrome('./chromedriver')
web.get(url)
time.sleep(3)
link = web.find_element_by_id('video-title')
link = link.get_attribute('href')
web.execute_script("window.open('https://www.youtubemp3.pro/en2', 'new_window')")
web.switch_to_window(web.window_handles[-1])
ylink = web.find_element_by_id("videoURL")
ylink.click()
ylink.send_keys(link)
submit = web.find_element_by_name("submitForm")
submit.click()
you do not have to use pyautogui it will work better if you use seleniu

try to find the element with it's ID.
import time
from selenium import webdriver
url = 'https://www.youtube.com/results?search_query=closer+8d'
web = webdriver.Chrome('../assets/chromedriver')
web.get(url)
time.sleep(3)
link = web.find_element_by_id('video-title')
link = link.get_attribute('href')
web.get('https://www.youtubemp3.pro/en2/')
time.sleep(3)
search_bar = web.find_element_by_id('txt-url')
search_bar.click()
search_bar.send_keys(link)

Related

Can't able to fetch span class text

Please find the attached image . I want to fetch the highlighted part in the image,
i want to fetch this attributes 1 · Trending ,#tuesdaymotivations,7,750 Tweets .
please advise.
URL to fetch=https://twitter.com/explore/tabs/trending
from selenium import webdriver
url = 'https://twitter.com/explore/tabs/trending'
# scrolling and scraping tweets
driver = webdriver.Chrome('/chromedriver')
driver.get(url)
trends = driver.find_element_by_xpath('//div[#data-testid="trends"]')
trend = trends[0]
trend.find_element_by_xpath('.//span').text
output:
Traceback (most recent call last):
File "/home/PycharmProject/Twitter_trending/ss.py", line 13, in <module>
trends = driver.find_element_by_xpath('//div[#data-testid="trends"]')
File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/remote/webdriver.py", line 394, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/remote/webdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.8/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[#data-testid="trends"]"}
(Session info: chrome=90.0.4430.72)
expected output:
1
·
Trending
#tuesdaymotivations
7,750 Tweets
2
·
Politics · Trending
#ModiResignOrRepeal
37.6K Tweets
3
·
Trending
#ThankfulTuesday
12.8K Tweets...etc
Try to fetch the text using JavaScript. Use this code.
span = trend.find_element_by_xpath('.//span')
text = driver.execute_script("return arguments[0].innerText", span)
This method can also provide text from nested elements. Still not working then try css selector instead of xpath.
trends = driver.find_elements_by_css_selector('div[data-testid="trends"]')
trend = trends[0]
span = trend.find_element_by_xpath('.//span')
text = driver.execute_script("return arguments[0].innerText", span)

Python Selenium: browser.get_log throws WebDriverException

I am trying to monitor the network in the IE Webdriver, the following is my code:
import time
import psutil
import os
import json
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import trackin_config
caps = DesiredCapabilities.INTERNETEXPLORER
caps['loggingPrefs'] = {'performance': 'ALL'}
browser = webdriver.Ie(trackin_config.WEB_DRIVER, desired_capabilities=caps)
browser.get(trackin_config.MES_WEBSITE)
def process_browser_log_entry(entry):
response = json.loads(entry['message'])['message']
return response
browser_log = browser.get_log('performance')
events = [process_browser_log_entry(entry) for entry in browser_log]
events = [event for event in events if 'Network.response' in event['method']]
print(events)
However I am getting the following error:
File "c:/Adhil/Python/MES_BOT/trackin-bot.py", line 43, in <module>
browser_log = browser.get_log('performance')
File "C:\Users\Adh\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 1262, in get_log
return self.execute(Command.GET_LOG, {'type': log_type})['value']
File "C:\Users\Adh\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\Adh\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 208, in check_response
raise exception_class(value)
selenium.common.exceptions.WebDriverException: Message:
Please advise, is there any package i need to install or configure?
errors = {}
for entry in context.browser.get_log('browser'):
errors.update(entry)

Error while executing javascript code in python3 while using selenium

I was trying to remove the ad and other pop-up using selenium after opening the webpage. The pop-up is getting removed but the ad is not getting removed. There is some error in executing javascript code(using it remove ad). For which there is no reason given. Also, for the ad, when I open the webpage myself by typing the link then ad does not appear but when I run the program, in code generated browser ad appears(reason I don't know). I have attached both code and error. Here is the code
from selenium import webdriver
import time
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("http://indianexpress.com/columnists/")
// To remove pop-up
later = driver.find_elements(By.CLASS_NAME, "iz_block_button")
later[0].click()
//to remove ad
all_iframes = driver.find_elements_by_tag_name("iframe")
if len(all_iframes) > 0:
print("Ad Found\n")
driver.execute_script("""
var elems = document.getElementsByTagName('iframe');
for(var i = 0, max = elems.length; i < max; i++)
{
elems[i].visibility=hidden;
}
""")
print('Total Ads: ' + str(len(all_iframes)))
else:
print('No frames found')
driver.close()
Error:-
Traceback (most recent call last):
File "/Users/arjungoyal/Desktop/untitled/a.py", line 23, in <module>
""")
File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 627, in execute_script
'args': converted_args})['value']
File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 312, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: hidden is not defined
(Session info: chrome=66.0.3359.181)
(Driver info: chromedriver=2.38.552518 (183d19265345f54ce39cbb94cf81ba5f15905011),platform=Mac OS X 10.13.4 x86_64)
Please someone tell what the error is and how to remove it.

Python Selenium error - is not clickable at point (207.1666717529297,25) because another element <div id="overley" class="over_ley"> obscures it

I have below code -
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("url")
username = driver.find_element_by_id("LoginID")
pwd = driver.find_element_by_id("Password")
username.send_keys('usrnme')
pwd.send_keys('pswd')
Login = driver.find_element_by_id("Button2")
Login.click()
Create = driver.find_element_by_xpath("//*[#title='Create Incident']")
Create.click()
Whenever I am running this code in script or in batch I am getting below error -
Traceback (most recent call last):
File "P:\Selenium\helpline1.py", line 15, in <module>
Create.click()
File "P:\Selenium\python-3.5.1\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "P:\Selenium\python-3.5.1\lib\site-packages\selenium\webdriver\remote\webelement.py", line 628, in _execute
return self._parent.execute(command, params)
File "P:\Selenium\python-3.5.1\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 312, in execute
self.error_handler.check_response(response)
File "P:\Selenium\python-3.5.1\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 237, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementClickInterceptedException: Message: Element <a id="anc" class="RedirectTab" href="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("anc", "", true, "", "", false, true))"> is not clickable at point (207.1666717529297,25) because another element <div id="overley" class="over_ley"> obscures it
Login to the site happens perfectly but after that this error in clicking an element (that is the next line of code).
Also when I am running this code line by line manually in Python IDLE or Python CLI it runs without any error. Issue just in script/batch mode in IDLE or Python CLI
Thanks to suggest.
If overlay displayed while you're trying to perform a click, you might need to wait until overlay is closed:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
...
Create = driver.find_element_by_xpath("//*[#title='Create Incident']")
WebDriverWait(driver, 10).until_not(EC.visibility_of_element_located((By.ID, "overley")))
Create.click()
Try this:
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
wait = WebDriverWait(driver, 10)
driver.get("url")
username = driver.find_element_by_id("LoginID")
pwd = driver.find_element_by_id("Password")
username.send_keys('usrnme')
pwd.send_keys('pswd')
Login = driver.find_element_by_id("Button2")
Login.click()
element = wait.until(EC.element_to_be_clickable((By.XPath, "//*[#title='Create Incident']")))
Create = driver.find_element_by_xpath("//*[#title='Create Incident']")
Create.click()
By usin a wait, you will give time to browser to load de button, BEFORE try to performe the click, that will happen when the element is prepared.

send_keys() giving error in python 3 selenium code with firefox

My code is:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
baseurl = "https://www.google.ca/?gfe_rd=cr&ei=J5ooWerXOsf_8AebtKKICw&gws_rd=ssl"
search = "panda"
xpaths = { 'searchbox' : ".//*[#id='lst-ib']",
'submit' : ".//*[#id='tsf']/div[2]/div[3]/center/input[1]",
'img' : ".//*[#id='gbw']/div/div/div[1]/div[2]/a"
}
driver = webdriver.Firefox()
driver.get(baseurl)
driver.find_element_by_xpath(xpaths['searchbox']).clear()
driver.find_element_by_xpath(xpaths['searchbox']).send_keys(search)
driver.find_element_by_xpath(xpaths['submit']).click()
#driver.find_element_by_xpath(xpaths['img']).click()
Firefox opens, but nothing at all happens, and written in the terminal is the following:
Traceback (most recent call last):
File "sg1.py", line 21, in <module>
driver.find_element_by_xpath(xpaths['searchbox']).send_keys(search)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webelement.py", line 347, in send_keys
self._execute(Command.SEND_KEYS_TO_ELEMENT, {'value': keys_to_typing(value)})
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webelement.py", line 494, in _execute
return self._parent.execute(command, params)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Expected [object Undefined] undefined to be a string
Things to note:
Firefox is up to date.
When Firefox opens it opens a plain version and not the version that usually opens with add-ons such as adblocker, firebug, etc.
When i ran just the click on 'img' bit that is commented out it did what it was supposed too.
This issue is common with geckodriver v.015; In order to resolve this update your geckodriver version to 0.16 also selenium to 3.4.0.

Resources