Selenium fails finding html element even though its visible - python

I wanted to program a selenium script with python which can write an input in an testbox but selenium doesnt find the element. (and i know the scipt looks real bad right now but please only concentrate on the error)
from selenium import webdriver
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
link = "https://kahoot.it/"
name = input("Name: ")
pin = str(input("PIN: "))
bots = int(input("Bots: "))
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10)
for i in range(bots):
time.sleep(1)
driver.execute_script('''window.open("https://kahoot.it/%22,%22_blank");''')
wait1 = wait.until(EC.visibility_of_element_located((By.XPATH, "/html/body/div\[1\]/div\[1\]/div/div/div/div\[3\]/div\[2\]/main/div/form/input")))
inputid = driver.find_element(By.NAME, 'game-Id')
inputid.send_keys(pin)
inputid.send_keys(Keys.ENTER)
time.sleep(5)
here is the console output:
Traceback (most recent call last):
File "c:\\Users\\Blackbird\\Documents\\programmieren\\kahootjoinbot\\main.py", line 19, in \<module\>
wait1 = wait.until(EC.visibility_of_element_located((By.XPATH, "/html/body/div\[1\]/div\[1\]/div/div/div/div\[3\]/div\[2\]/main/div/form/input")))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\\Python311\\Lib\\site-packages\\selenium\\webdriver\\support\\wait.py", line 95, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
I tried some other things like finding the element by Name or ID or css selector but it all didnt work.

With this should work:
from selenium import webdriver
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
name = input("Name: ")
pin = str(input("PIN: "))
bots = int(input("Bots: "))
driver = webdriver.Chrome()
for i in range(bots):
driver.execute_script('''window.open("https://kahoot.it/%22,%22_blank");''')
driver.switch_to.window(driver.window_handles[i+1])
wait = WebDriverWait(driver, 10)
input = wait.until(EC.visibility_of_element_located((By.XPATH, "//input[#name='gameId']")))
input.send_keys(name)
input.send_keys(Keys.ENTER)
time.sleep(5)
Why is your code not working?
Because you are using line driver.execute_script('''window.open("https://kahoot.it/%22,%22_blank");''').
That line is opening a new tab, then when you do your next line wait1 = wait.until(EC.visibility_of_element_located((By.XPATH, "/html/body/div\[1\]/div\[1\]/div/div/div/div\[3\]/div\[2\]/main/div/form/input"))) your are using the first tab, but you navigate in the second tab.
So, you are opening a second tab and trying to locate your element in your fist tab, that's why no matters what element you try to find in your first tab, because your first tab does not contain anything.
How to solve it? Navigate in your first tab using driver.get("https://kahoot.it/")
Or you can change to your new tabs using: driver.switch_to.window(driver.window_handles[i+1]) where [i+1] is the index of tabs you have. So first tab is 0 second is 1 and so on.
Advices:
Do not use full xpaths, in the moment something change in the page your xpath will probably not be valid anymore
You can wait for the element and save in a variable, as I did, then just use it for sendkeys, click etc.

Related

I am scraping the following website which contains java script but getting an error

I am trying to scrape a website but when I try to run the program, I get the following error. Here is my code
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.expected_conditions import presence_of_element_located
driver = webdriver.Chrome(executable_path = '/home/danish-khan/webscraping/rgcrawler2/chromedriver')
driver.get('https://www.researchgate.net/institution/Islamia_College_Peshawar/department/Department_of_Computer_Science/members')
chrome_options = Options()
element = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, '//*[#id="rgw9_5fac070727fc2"]/div[3]/h5/a]')))
print(element.text)`
Traceback (most recent call last):
File "resgt3.py", line 14, in <module>
element = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, '//*[#id="rgw9_5fac070727fc2"]/div[3]/h5/a]')))
File "/home/danish-khan/miniconda3/lib/python3.7/site-packages/selenium/webdriver/support/wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
element = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, '//*[#id="rgw9_5fac070727fc2"]/div[3]/h5/a]')))
While page is loading your program waits 20 seconds for XPATH element '//*[#id="rgw9_5fac070727fc2"]/div[3]/h5/a]' to appear.
If the XPATH element doesn't appear after defined time you get a Timeout error. Which is a good thing, otherwise your program would stuck forever if the XPATH element won't appear at all.
I think you should double check whether the provided XPATH is correct, or that it doesn't change over time.

Python Selenium: Finds element but returns empty string

I am trying to get all players from this Page:
https://earthmc.net/map
(You can see them when you click on the arrow on the right side).
The tags for the players looks like this:Blu_Viper
I am using this code for this:
driver = webdriver.Firefox()
driver.get("https://earthmc.net/map/")
name = driver.find_elements_by_xpath('//a[#title="Center on player"]')
for i in range(len(name)):
print(name[i].text)
driver.close()
The driver finds the Xpath but it prints only a blank string.
How can I fix this?
You can use this code:- Moreover, you can change the sleep timing as per your need. for your information, this code gives error sometimes because the list is pretty dynamic. But you can run it again to get the full player list.
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
driver = webdriver.Chrome()
action = ActionChains(driver)
driver.get('https://earthmc.net/map/')
wait = WebDriverWait(driver, 20)
time.sleep(20)
ExpandArrow = wait.until(EC.visibility_of_element_located((By.XPATH, '//div[#class="hitbar"]')))
action.move_to_element(ExpandArrow).perform()
PlayerList = driver.find_elements_by_xpath('//li[contains(#class,"player")]')
for element in PlayerList:
driver.execute_script("arguments[0].scrollIntoView();", element)
print(element.text)
print("Successfully Completed.")
Note - If this is what you are looking for then mark this as the answer.

Selenium timeoutexception with webdriver

First post on here and brand new to Python. I am trying to learn how to scrape data from a website. When you first load the website, a disclaimer window shows up and all I am trying to do is hit the accept button using the browser.find_element_by_id.
I am using the webdriverwait command to wait for the page to load prior to clicking the "Accept" button but I keep getting a Timeoutexception. Here is the code that I currently have:
from selenium import webdriver
#get the chrome webdriver path file
browser = webdriver.Chrome(executable_path=r"C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe")
browser.get('http://foreclosures.guilfordcountync.gov/')
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
#wait until element is loaded
wait = WebDriverWait(browser, 10)
wait.until(EC.presence_of_element_located((By.ID, "cmdAccept")))
element = browser.find_element_by_id("cmdAccept")
element.click()
Here is the error I keep getting:
Traceback (most recent call last):
File "C:/Users/Abbas/Desktop/Foreclosure_Scraping/Foreclosure_Scraping.py", line 33, in <module>
wait.until(EC.presence_of_element_located((By.ID, "cmdAccept")))
File "C:\Users\Abbas\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
I believe it has something to do with the calling out the ID of the button itself from the website but I honestly do not know. Any help is greatly appreciated.
Your attempts to locate the element are unsuccessful because of they are nested within an iframe. One must tell selenium to switch to the iframe that contains the desired element before attempting to click it or use it in any way. Try the following:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
#get the chrome webdriver path file
browser = webdriver.Chrome(executable_path=r"C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe")
browser.get('http://foreclosures.guilfordcountync.gov/')
browser.switch_to.frame(browser.find_element_by_name("ctl06"))
wait = WebDriverWait(browser, 10)
wait.until(EC.presence_of_element_located((By.ID, "cmdAccept")))
element = browser.find_element_by_id("cmdAccept")
element.click()

python and selenium send keys

Im trying to add input to a text box when i do try it doesn't find the element and it gives an error i don't know if im selecting the right element this is what i have so far
Traceback (most recent call last):
File "./fl_bot.py", line 22, in <module>
ui.WebDriverWait(browser, 10).until(EC.visibility_of_element_located((By.ID, "#billFirstName")))
File "/Library/Python/2.7/site-packages/selenium/webdriver/support/wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
the code abouve is the error message im getting
from selenium.webdriver.support import ui
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
def get_page(model, sku):
url = "https://www.footlocker.com/product/model:"+str(model)+"/sku:"+ str(sku)+"/"
return url
browser = webdriver.Firefox()
page=browser.get(get_page(277097,"8448001"))
browser.find_element_by_xpath("//*[#id='pdp_size_select_mask']").click()
shoesize = ui.WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'a.grid_size:nth-child(8)')))
shoesize.click()
browser.find_element_by_xpath("//*[#id='pdp_addtocart_button']").click()
checkout = browser.get('https://www.footlocker.com/shoppingcart/default.cfm?sku=')
checkoutbutton = browser.find_element_by_css_selector('#cart_checkout_button').click()
ui.WebDriverWait(browser, 10).until(EC.visibility_of_element_located((By.ID, "#billFirstName")))
browser.find_element_by_id("#billFirstName").send_keys(Keys.RETURN)
every time it makes it to the end of the script it dosent type and it just stops
[1]: https://www.footlocker.com/checkout/?uri=checkout this is the page im trying to check out on
The FIRST NAME is a mandatory field to be filled up, so instead of send_keys(Keys.RETURN) try to send some text as follows along with the expected_condition as element_to_be_clickable :
ui.WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[#id='billFirstName']")))
browser.find_element_by_xpath("//input[#id='billFirstName']").click()
browser.find_element_by_xpath("//input[#id='billFirstName']").clear()
browser.find_element_by_xpath("//input[#id='billFirstName']").send_keys("user_first_name")

selenium and python3: selecting search box on www.rottentomatoes.com

I have a list of movies for which I want to get the reviews from rotten www.rottentomatoes.com, but I have run into a snag.
What I want is to be able to pass the title of each movie to website search box and then process the result to get the review I want.
At present, I cannot get beyond the search stage, because I have not been able to successfully locate the search box.
My code is as shown below:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
browser = webdriver.Chrome('/home/zona/chromedriver')
url = 'https://www.rottentomatoes.com/'
browser.get(url)
time.sleep(10)
try:
element = WebdriverWait(browser, 10).until(
EC.presence_of_element_located((By.XPATH,'//body//input[#name="search"]')))
element = browser.find_element_by_xpath('//body//input[#name="search"]')
element.clear()
element.send_keys("avatar")
except:
print("cound not find search box")
time.sleep(5)
browser.quit()
I get the output:
cound not find search box
Can someone please help me locate what I am doing wrong?
Apologies if this is too basic please, I am new to programming and to python.
It is just case-sensitivity issue.
You used WebdriverWait (lower case d) instead of WebDriverWait.
Note: Used trackback module to print the stack trace to know the exception details.
Try the following code:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import traceback
browser = webdriver.Chrome(`/home/zona/chromedriver`)
url = 'https://www.rottentomatoes.com/'
browser.get(url)
time.sleep(5)
try:
element = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH,'//body//input[#name="search"]')))
# element = browser.find_element_by_xpath('//body//input[#name="search"]')
element.clear()
element.send_keys("avatar")
except:
traceback.print_exc()
print("cound not find search box")
time.sleep(5)
browser.quit()

Categories