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)
Related
This question already has answers here:
find_element_by_* commands are deprecated in Selenium
(4 answers)
Closed 7 months ago.
My code opens driver and opens website but gives error about:
AttributeError: 'WebDriver' object has no attribute 'find_element_by_name'
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver=webdriver.Firefox()
driver.get("https://www.python.org")
time.sleep(5)
search_bar = driver.find_element_by_name("q")
search_bar.clear()
search_bar.send_keys("getting started with python")
search_bar.send_keys(Keys.RETURN)
driver.close()
Use ID to find element
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
driver=webdriver.Firefox()
driver.get("https://www.python.org")
time.sleep(5)
search_bar = driver.find_element(By.ID, "id-search-field")
search_bar.clear()
search_bar.send_keys("getting started with python")
search_bar.send_keys(Keys.RETURN)
driver.close()
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)
This question already has an answer here:
How to test non-standard drop down lists through a crawler using Selenium and Python
(1 answer)
Closed 2 years ago.
I'm trying to make my bot to click on a drop down menu, select option A and refresh the page. The problem I'm having is that I can't use Select as I get error message saying that it can't use select on . Here is the inspection of the drop down in question. How do I make it to select option A?
<div class="Select-value"><span class="Select-value-label" role="option" aria-selected="true"
id="react-select-2--value-item">CrazyFortune</span></div>
I can't share the website as it contains sensitive info but here is my code:
import pandas as pd
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import Select
import openpyxl
import xlsxwriter
#my private module with sensitive info
import pwx_module as pwx
#delay libraries
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
#imports
driver_path = r'C:\Users\name\chromedriver.exe'
outcome = openpyxl.load_workbook(r'C:\Users\name\outcome.xlsx')
driver = webdriver.Chrome(driver_path)
driver.get('link_hidden')
# waiting for login table to load
try:
element = WebDriverWait(driver,10).until(
ec.presence_of_element_located((By.XPATH,'//*[#id="email"]'))
)
except:
driver.quit()
#entering sensitive info
driver.find_element_by_id("email").send_keys(pwx.em)
driver.find_element_by_id("password").send_keys(pwx.pw)
driver.find_element_by_xpath('//*
[#id="appContainer"]/div/form/button').click()
# waiting for page to load
try:
element = WebDriverWait(driver,10).until(
ec.presence_of_element_located((By.XPATH,'//* [#id="testing"]/section/section[1]/div[1]/header/div/div/div[1]/div[2]/div''))
)
except:
driver.quit()
# THIS IS WHERE THE PROBLEM IS!!! - code above works fine
drp = select(driver.find_element_by_xpath('//*
[#id="testing"]/section/section[1]/div[1]/header/div/div/div[1]/div[2]/div'))
drp.select_by_visible_info('A')
driver.refresh()
# writing info into excel file, code below works fine
cell = outcome['import']
withdrawal_cell = 'B19'
cell[withdrawal_cell].value = w.text
driver.quit()
outcome.save(r'C:\Users\giuse\Desktop\Back office bot\outcome.xlsx')
Try to pass the correct xpath
driver.find_element_by_xpath("//div/span[#id='react-select-2--value-item']").click()
I'm trying to add a repetitive text response to a chat box in a gaming website in order to create an algorithm which guesses words based on a drawing and hint.
The website is the popular pictionary website : https://skribbl.io/
I've been working on the algorithm to guess the words based on others reply, I'm not familiar with Selenium and trying to just print some simple text in the chat/guess textbox.
The website opens up, but it's not printing anything onto the box. How can I resolve this ? Thank you
from selenium import webdriver
from selenium.webdriver.support import ui
from selenium.webdriver.common.keys import Keys
def page_is_loaded(driver):
return driver.find_element_by_tag_name("body")!=None
driver = webdriver.Firefox(executable_path = 'C:\Program Files\gecko\geckodriver.exe')
driver.get("https://skribbl.io/?p0YRvXqupiza")
wait = ui.WebDriverWait(driver,10)
wait.util(page_is_loaded)
for x in range (0,20):
textbox = driver.find_element_by_name("text")
textbox.send_keys("1")
This is how the homepage of Skribbl.io looks like - https://i.imgur.com/Udth9vs.jpg
The textbox is seen at the bottom right-hand side where I want the input from my code can be found here - https://i.imgur.com/frMTFjJ.jpg
Try the following
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
firefox_browser = webdriver.Firefox(executable_path=r'C:\Program Files\gecko\geckodriver.exe')
firefox_browser.get("https://skribbl.io/")
time.sleep(2)
name_input = firefox_browser.find_element_by_css_selector("#inputName")
play_button = firefox_browser.find_element_by_css_selector("button.btn:nth-child(3)")
name_input.send_keys("Drums3")
play_button.send_keys(Keys.ENTER)
for x in range(0, 20):
time.sleep(3)
chat_input = firefox_browser.find_element_by_css_selector("#inputChat")
chat_input.send_keys("hello")
chat_input.send_keys(Keys.ENTER)
Yes i know that this type of question has been answered many times before, but none of them helped me. Actually i didn't know much about it so need your help!
My problem:
I am scraping through a website and it needs a CAPTCHA to search for every input. So i use FireFox as my browser as it asks for the captcha one time and doesn't change it. My code asks the user for CAPTCHA one time and then click on search button and tries to scrape the data, but when it clicks on the search button again (as it is in a loop) then it raises this error:
selenium.common.exceptions.StaleElementReferenceException:
Message: The element reference of <input id="txt_ALPHA_NUMERIC" class="ui-inputfield ui-inputtext ui-widget ui-state-default ui-corner-all" name="txt_ALPHA_NUMERIC" type="text"> is stale;
either the element is no longer attached to the DOM, it is not in the current frame context, or the document has been refreshed
My old code:
from selenium import webdriver # Import module
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys # For keyboard keys
import time
import pandas as pd
URL = 'https://vahan.nic.in/nrservices/faces/user/searchstatus.xhtml' # Define URL
browser = webdriver.Firefox(executable_path=r'C:\Users\intel\Downloads\Setups\geckodriver.exe')
browser.get(URL)
vehicle_no = browser.find_element_by_xpath("""//*[#id="regn_no1_exact"]""")
vehicle_no.send_keys('RJ14CX3238')
captcha_input = input("enter your captcha ")
captcha = browser.find_element_by_xpath("""//*[#id="txt_ALPHA_NUMERIC"]""")
captcha.send_keys(captcha_input)
button_click = browser.find_element_by_xpath("/html/body/form/div[1]/div[3]/div/div[2]/div/div/div[2]/div[5]/div/button/span").click()
i = 111
attempt = 1
max_attempts = 2
while True:
i = i + 1
time.sleep(4)
reg_no = browser.find_element_by_xpath("/html/body/form/div[1]/div[3]/div/div[2]/div/div/div[2]/div[6]/div/div/div/table/tbody/tr[2]/td[2]/span").text
date = browser.find_element_by_xpath("/html/body/form/div[1]/div[3]/div/div[2]/div/div/div[2]/div[6]/div/div/div/table/tbody/tr[2]/td[4]").text
vehicle_no = browser.find_element_by_xpath("""//*[#id="regn_no1_exact"]""")
vehicle_no.send_keys('RJ14CX3' + str(i))
captcha.send_keys(captcha_input)
button_click = browser.find_element_by_xpath("/html/body/form/div[1]/div[3]/div/div[2]/div/div/div[2]/div[5]/div/button/span").click()
browser.execute_script("return arguments[0].scrollIntoView(true);", button_click)
Updated new code now:
from selenium import webdriver # Import module
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys # For keyboard keys
import time
import pandas as pd
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
URL = 'https://vahan.nic.in/nrservices/faces/user/searchstatus.xhtml' # Define URL
browser = webdriver.Firefox(executable_path=r'C:\Users\intel\Downloads\Setups\geckodriver.exe')
browser.get(URL)
vehicle_no = browser.find_element_by_xpath("""//*[#id="regn_no1_exact"]""")
vehicle_no.send_keys('RJ14CX3238')
captcha_input = input("enter your captcha ")
captcha = browser.find_element_by_xpath("""//*[#id="txt_ALPHA_NUMERIC"]""")
captcha.send_keys(captcha_input)
button_click = browser.find_element_by_xpath("/html/body/form/div[1]/div[3]/div/div[2]/div/div/div[2]/div[5]/div/button/span").click()
i = 111
while True:
button_click = browser.find_element_by_xpath("/html/body/form/div[1]/div[3]/div/div[2]/div/div/div[2]/div[5]/div/button/span")
WebDriverWait(browser, 10).until_not(EC.visibility_of_element_located((By.ID, "overley")))
browser.execute_script("return arguments[0].scrollIntoView(true);", button_click)
i = i + 1
#reg_no = browser.find_element_by_xpath("/html/body/form/div[1]/div[3]/div/div[2]/div/div/div[2]/div[6]/div/div/div/table/tbody/tr[2]/td[2]/span").text
#date = browser.find_element_by_xpath("/html/body/form/div[1]/div[3]/div/div[2]/div/div/div[2]/div[6]/div/div/div/table/tbody/tr[2]/td[4]").text
time.sleep(5)
vehicle_no.send_keys('RJ14CX3' + str(i))
WebDriverWait(browser, 10).until_not(EC.visibility_of_element_located((By.ID, "overley")))
captcha.send_keys(captcha_input)
Also fix any other problems if is in my code. Any help would be appreciated!!
Thanks in advance.
Simply re-find the button element in the loop, each time, rather than before the loop starts. Any time the DOM mutates, previous references are marked as stale, and will require a new instance. Interacting with Captcha's mutate the DOM, and mark the page as dirty (having changed/modified), which Selenium uses to flag "staleness".