Element's Click Intercepted [duplicate] - python

This question already has answers here:
org.openqa.selenium.ElementClickInterceptedException: element click intercepted error using Selenium and Java in headless mode
(8 answers)
Closed 1 year ago.
i've been trying to make selenium click a button for me but all i get is element click intercepted
i first thought about exception handling but i want to know if there's a better way
eitherway i am a bit confused
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from win10toast import ToastNotifier as Notifier
from selenium.webdriver.common.by import By
from selenium import webdriver
from time import sleep
Path: str = "C:\\Users\\rolan\\Desktop\\chromedriver.exe"
Link: str = "https://www.roblox.com"
Notification = Notifier()
Notification.show_toast("hulks bot", "settings things up...")
sleep(1)
Option = Options()
Option.add_argument("start-maximized")
Browser = webdriver.Chrome(service = Service(Path), options = Option)
Browser.get(Link)
def Perform_Actions():
sleep(1)
Login_Button = Browser.find_element(By.ID, "main-login-button")
Login_Button.click()
Perform_Actions()```
much appreciated help

fixed all you needed to do was just click on the main container of everything(just the screen)

Related

Selenium closes browser after Python code is finished filling out form [duplicate]

This question already has answers here:
Python selenium keep browser open
(4 answers)
Closed 5 months ago.
I'm using Selenium to auto-fill forms using forminfo.py for the inputted information. (Right now I'm testing it out on Instagram just to figure everything out.) What I have done is, I have it check to see if the google chrome driver is already installed in a particular location and if not it installs it and if so it just continues and runs the code to fill out the form.
The problem I'm having is, everything runs fine IF the driver isn't installed and it installs it manually. BUT If the driver is already installed, after the form is filled out and it hits the submit button it closes the browser immediately. Ive only been learning Python for about a few weeks now and I cant figure out whats wrong. Also every where I've look I can find a solution to the problem. Also, I'm not getting any errors in my terminal.
################## IMPORTS ##################
import forminfo
import os
import urllib.request as urllib
import webbrowser
import zipfile
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.proxy import Proxy, ProxyType
from time import sleep
################## CHROME DRIVER DOWNLOAD ##################
path = "C:/Users/"+os.getlogin()+"/Documents/chromedriver.exe"
isFile = os.path.isfile(path)
if not os.path.isfile(path):
webbrowser.get('windows-default').open("http://www.google.com", new=0)
zip_path, _ = urllib.urlretrieve(forminfo.downloadurl)
urllib.urlopen = zip_path
extract_dir = "C:/Users/"+os.getlogin()+"/Documents"
zipfile.ZipFile(zip_path, "r").extractall(extract_dir)
else:
print("Starting now....")
################## PROXY INFO ##################
prox = Proxy()
prox.proxy_type = ProxyType.MANUAL
prox.socks_version = 5
prox.socks_proxy = forminfo.socks5ip
#prox.http_proxy = "ip_addr:port"
#prox.ssl_proxy = "ip_addr:port"
################## BROWSER ##################
capabilities = webdriver.DesiredCapabilities.CHROME
prox.add_to_capabilities(capabilities)
#
capabilities['acceptInsecureCerts'] = True
capabilities['acceptSslCerts'] = True
#
driver_service = Service(executable_path="C:/Users/"+os.getlogin()+"/Documents/chromedriver")
chrome_options = Options()
chrome_options.add_argument("--incognito")
chrome_options.add_argument("start-maximized")
chrome_options.add_experimental_option("useAutomationExtension", False)
chrome_options.add_experimental_option("excludeSwitches",["enable-automation"])
#
chrome_options.add_argument("--ignore-certificate-error")
chrome_options.add_argument("--ignore-ssl-errors")
#
driver = webdriver.Chrome(service=driver_service, options=chrome_options, desired_capabilities=capabilities)
driver.get('https://www.instagram.com')
################## BOT ##################
sleep(3)
elem = driver.find_element(By.NAME, "username")
elem.send_keys(forminfo.username)
elem = driver.find_element(By.NAME, "password")
elem.send_keys(forminfo.password)
sleep(3)
elem = driver.find_element(By.XPATH, "//*[#id=\"loginForm\"]/div/div[3]/button/div")
elem.click()
The only solution Ive found to the problem is here by adding a breakpoint() at the very end of my code. But I feel like this would mess some things up down the road if I decided to expand on this code and eventually turn it into a .exe file with pyIntsaller or Nuitak. So I wanted to see If there was a solution before just moving forward with that resolution.
I know it's a problem with the ################## CHROME DRIVER DOWNLOAD ################## code because I wasn't having this problem until I decided to add that code. This Is the very most recent part I added and everything was fine until then.
Converting comment to an answer.
You can use the detach option to let the browser Window stay open after using it with Selenium, example:
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)

How to click on element inside shadowroot using selenium

I am using a captcha bypass extension called "Buster: Captcha Solver for Humans
1.3.1". Works great but cant seem to click on its button for this website because its blocked by a shadowroot. I have no experience in bypassing shadowroots so please any help would be GREATLY appreciated. I have been working for this on hours and still have no luck. Below is a snippet of the code I am making for this site. The site is https://www.biblegateway.com/newsletters/ and the captcha in the shadow root appears when you enter a email and click on subscribe. I posted pics to help describe this problem as well!
from lib2to3.pgen2 import driver
import random
import string
import selenium
from selenium import webdriver
from selenium.webdriver.chrome import options
from selenium.webdriver.common.keys import Keys
import time
from time import sleep, time_ns
import pynput
import threading
from threading import Thread
from pynput.keyboard import Key, Controller
from selenium.webdriver.chrome.options import Options
PATH = "C:\Program Files (x86)\chromedriver.exe"
chrome_options1 = Options()
chrome_options1.add_extension(r'D:\mpbjkejclgfgadiemmefgebjfooflfhl.crx')
driver1 = webdriver.Chrome(executable_path= PATH, options=chrome_options1)
driver1.get("https://www.biblegateway.com/newsletters/")
driver1.maximize_window()
email_box = driver1.find_element_by_xpath('/html/body/div[2]/div/section/div[3]/section/form/div/div/div[2]/div[2]/div[1]/input')
email_box.click()
email_box.send_keys('testingthecaptcha#gmail.com')
time.sleep(2)
clicky = driver1.find_element_by_xpath('/html/body/div[2]/div/section/div[3]/section/form/div/div/div[1]/div[1]/div[2]/label[1]/input')
clicky.click()
time.sleep(3)
clicky1 = driver1.find_element_by_xpath('/html/body/div[2]/div/section/div[3]/section/form/div/div/div[2]/div[2]/div[2]/input')
clicky1.click()
print('done')
time.sleep(2)
#Below is where I just could not click the element or bypass the shadowroot!!!!!!!
captcha_solver = driver1.find_element_by_xpath("//input[#placeholder='Instrument']")
captcha_solver.click()
time.sleep(3)
print('done')
Using selenium 4:
shadow_root = driver.find_element(By.XPATH, 'shadow-root-parent-element').shadow_root
element_in_shadow_root = shadow_root.find_element(By.XPATH, 'element-in-shadow-root')
e.g.
shadow_root = driver.find_element(By.CSS_SELECTOR, 'game-app').shadow_root
game = shadow_root.find_element(By.CSS_SELECTOR, '#game')

Selenium closing the brower alone after finishing all the function

I'm pretty new in programming so I might be an easy question, but I don't understand why the browsers opened by Selenium closes at the end of the code.
from lib2to3.pgen2 import driver
from selenium import webdriver
def Online_PLatform():
Driver = webdriver.Chrome()
Driver.get('https://elearningmarikina.ph/')
Gmail = Driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/div[2]/div/div/form/div[1]/input')
Gmail.send_keys('958rectin#depedmarikina.com')
Pass = Driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/div[2]/div/div/form/div[2]/input')
Pass.send_keys('33112')
Button = Driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/div[2]/div/div/form/div[3]/button')
Button.click()
You can use 2 approaches in order to keep you driver open.
1.
Add the 'detach' option to your driver settings:
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
Simply add a delay at the end of your test code (less elegant approach but more simple)
from lib2to3.pgen2 import driver
from selenium import webdriver
import time
def Online_PLatform():
Driver = webdriver.Chrome()
Driver.get('https://elearningmarikina.ph/')
Gmail = Driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/div[2]/div/div/form/div[1]/input')
Gmail.send_keys('958rectin#depedmarikina.com')
Pass = Driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/div[2]/div/div/form/div[2]/input')
Pass.send_keys('33112')
Button = Driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/div[2]/div/div/form/div[3]/button')
Button.click()
time.sleep(50)
This is because after the all functions, The code stops running and that's why selenium exits.
You can use the time module to delay.
import time
from lib2to3.pgen2 import driver
from selenium import webdriver
def Online_PLatform():
Driver = webdriver.Chrome()
Driver.get('https://elearningmarikina.ph/')
Gmail = Driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/div[2]/div/div/form/div[1]/input')
Gmail.send_keys('958rectin#depedmarikina.com')
Pass = Driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/div[2]/div/div/form/div[2]/input')
Pass.send_keys('33112')
Button = Driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/div[2]/div/div/form/div[3]/button')
Button.click()
time.sleep(50) #---> 50 second delay

Python - How do I switch between tabs in selenium [duplicate]

This question already has answers here:
Selenium Switch Tabs
(4 answers)
Closed 2 years ago.
I'm new to python and selenium and I'm trying to switch between tabs, I've already tried using the normal key commands, ActionChains, and Keys.CONTROL, but it's not working, how would I do this. The program is the following
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import webbrowser
import time
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import multiprocessing
driver1 = webdriver.Chrome(executable_path="C:\mydriver\chromedriver")
ar = ['https://google.com', 'https://bing.com']
for page1 in (ar):
driver1.execute_script(f"window.open ('{page1}')")
time.sleep(3)
for x in range (5):
driver1.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.TAB)
take a look at this guide:
https://www.browserstack.com/guide/how-to-switch-tabs-in-selenium-python
But this is the key part:
#get current window handle
p = driver.current_window_handle
#get first child window
chwd = driver.window_handles
for w in chwd:
#switch focus to child window
if(w!=p):
driver.switch_to.window(w)
break
time.sleep(0.9)
print("Child window title: " + driver.title)

Selenium / Python switch webdriver from headless to window mode [duplicate]

This question already has answers here:
How do I make Chrome Headless after I login manually
(2 answers)
Closed 4 years ago.
Is there any way to switch Chrome webdriver from headless mode to window mode?
One thing that came to my mind is to 'switch' existing web driver to non-headless mode.
Another idea: to create new instance of webdriver (this time non-headless) with some sort of 'state' from old one so the user operations can be executed. I don't know how to do or if it is possible though.
import os
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException,
options = webdriver.ChromeOptions()
options.add_argument('headless')
driver = webdriver.Chrome(
executable_path=os.path.join(os.getcwd(), 'chromedriver'),
chrome_options=options,
)
driver.get('https://website.com')
try:
driver.find_element_by_xpath('//h1').click()
except NoSuchElementException:
print('You have to click it manually')
# here I need Chrome browser
# to be opened so that I can click a link
print('The name of this thing is: ', end='')
print(driver.find_element_by_xpath("//h1[#class='name']").text)
If you need to open a new tab
driver.execute_script("window.open()")
If you need to switch to this new one
driver.switch_to.window(self.driver.window_handles[1])
Then you get the page
driver.get('https://website.com')
and the end you can close it (the new one)
driver.close()
and you back to the first driver
switch_to.window(driver.window_handles[0])

Categories