Selenium ChromeDriver python script can no longer find any elements on page - python

Selenium newb here. I've had a very simple python script to fill out a questionnaire working for about 3 weeks. I just noticed it was getting stuck on the 3rd question today, and when I cleared cache and rebooted, Selenium seemingly can't find any element I try using Full or short XPATH on that page any longer.
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element:
I spent some time trying to match up ChromeDriver and Chrome Browser versions assuming that it auto-updated. However, I tested the same previously working script on a different website and it's working fine. So not sure if this small version difference is really the problem
Driver = 94.0.4606.61
Chrome = 94.0.4606.81
I checked for iframes, but can't seem to locate one under source or inspect. Here is the web form:
https://form.typeform.com/to/eUIsSKGd
I've been using hard.coded sleep since the beginning and it was working fine. I even extended the sleeps to exaggerate them, but still can't get past the "Start Button".
Here's the first snippet of the script that was working. Yes, I tried grabbing a fresh new XPATH
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get('https://form.typeform.com/to/eUIsSKGd')
time.sleep(5)
#StartButton
startbutton = driver.find_element_by_xpath('/html/body/div[3]/main/div[1]/div[2]/div/section/div[2]/div[1]/div')
startbutton.click()
Please help

Related

Selenium find_element_by_css_selector not finding element

I'm working on a personal project to automate the joining of a Twitch channel through Selenium using Python. This is my first time using Selenium and I've run into an issue that I can't seem to resolve.
When joining a channel that is flagged as containing mature content, it requires a user to click a button to start watching the stream. My program works great until the button needs to be clicked.
The HTML for button appears as:
<button class="ScCoreButton-sc-1qn4ixc-0 ScCoreButtonPrimary-sc-1qn4ixc-1 jnsGXs ksFrFH" data-a-target="player-overlay-mature-accept"><div class="ScCoreButtonLabel-sc-lh1yxp-0 evtFDZ"><div data-a-target="tw-core-button-label-text" class="Layout-sc-nxg1ff-0 eZactg">Start Watching</div></div></button>
Since the class name appear to be dynamically generated, I planned on the using the "data-a-target" as the selector. Looking at Selenium's docs, it appeared that the find_element_by_css_selector function is what I need to use. I played around in the Chrome developer tools and was about to select the button using: document.querySelector('button[data-a-target="player-overlay-mature-accept"]'), so I was confident that it would work with Selenium.
I made the selection in my code with Selenium using the following code:
mature = driver.find_element_by_css_selector('button[data-a-target="player-overlay-mature-accept"]')
Unfortunately, Selenium doesn't seem to be able to find it and returns the error message:
Message: no such element: Unable to locate element: {"method":"css selector","selector":"button[data-a-target="player-overlay-mature-accept"]"}
(Session info: chrome=97.0.4692.71)
Am I doing something blatantly wrong? I've spent a good amount of time trying different things but nothing seems to work.
Maybe this:
mature = driver.find_element_by_xpath("//button[#data-a-target='player-overlay-mature-accept']")
or
mature = driver.find_element_by_xpath("//button[contains(#data-a-target, 'mature')]")

Is there any way to send commands (like ":screenshot") to the firefox console with Selenium using Python?

I am working on an automation task that opens a webpage, screenshots an element from the page, then closes the page and moves on to the next. I had previously set this up using AutoHotKey and, although it worked technically, I wanted to create a more refined version. Selenium has worked well up to now for automating the navigation of pages, but when I would be ready to take my screenshot, I can't seem to get the console open to issue the command. I tried using the
from selenium.webdriver.common.keys import Keys
browser.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.SHIFT + 'K')
command but it seems that firefox doesn't receive the input. I also tried
from selenium.webdriver.common.action_chains import ActionChains
actions = ActionChains(browser)
actions.send_keys(Keys.CONTROL + Keys.SHIFT + 'k')
which, again, didn't seem to work.
Lastly I tried using
browser.execute_script(":screenshot")
but I kept getting a JavaScript error which makes sense since the screenshot command isn't js.
If there's anything you can think of that I am overlooking please let me know! Thanks in advance :)
Selenium screenshot
Example
You can take a screenshot of a webpage with the method get_screenshot_as_file() with as parameter the filename.
The program below uses firefox to load a webpage and take a screenshot, but any web browser will do.
from selenium import webdriver
from time import sleep
driver = webdriver.Firefox()
driver.get('https://www.python.org')
sleep(1)
driver.get_screenshot_as_file("screenshot.png")
driver.quit()
print("end...")
The screenshot image will be stored in the same directory as your Python script. Unless you explicitly define the path where the screenshot has to be stored.

Running into errors following a selenium chromedriver web automation tutorial

I'm fairly new to programing and have been trying my best to get a grasp of different concepts. I've made a few games and twitter bots, and am looking at trying some web scraping/automation.
I have been following along with this tutorial https://www.youtube.com/watch?v=f7LEWxX4AVI
and have gotten stuck trying to enter text into the search bar. I'm using chrome Version 81.0.4044.129 and have the right Chromedriver to go with it.
This is my code from the tutorial -
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://youtube.com')
searchbox = driver.find_element_by_xpath('//*[#id="search"]')
searchbox.send_keys('Type Type Type')
searchButton = driver.find_element_by_xpath('//*[#id="search-icon-legacy"]')
searchButton.click()
It opens up chrome but does not enter anything into the search bar
📷
https://gyazo.com/68da5e2e2340265ae50125558ed8ed11
I get these errors that open in a new window
and I get these in my python shell
📷
https://gyazo.com/e9d702ef1ec4e4bc422ae537a3b59fbe
any advice would be very much appreciated.

Scroll down section of webpage with selenium/python

I'm trying to scroll down a specific div in a webpage (ticker box in facebook) using selenium (with python).
I can find the element, but when i try to scroll it down using:
header = driver.find_element_by_class_name("tickerActivityStories")
driver.execute_script('arguments[0].scrollTop = arguments[0].scrollHeight', header)
Nothing happens.
I've found a lot of answers, but mostly are for selenium in java, so i would like to know if it's possible to do it from python.
I've found a lot of answers, but mostly are for selenium in java
There is no difference, selenium has the same api in java and python, you just need to find the same function in python selenium.
First of all check if your JS works in the browser(if not, try scroll parent element).
Also you can try :
from selenium.webdriver.common.keys import Keys
header.send_keys(Keys.PAGE_DOWN)

Using AutoIT with Selenium

Thank you for answering my previous question but as one is solved another is found apparently.
Interacting with the flash game itself is now the problem. I have tried researching how to do it in Selenium but it can't be done. I've seen FlashSelenium, Sikuli, and AutoIT.
I can only find documentation for FlashSelenium in Java, It's easier for me to use AutoIT rather than Sikuli as I'd have to learn to use Jpython to create the kind of script I want to, which I am not straying away from learning just trying to finish this as fast as possible. As for AutoIT, the only problem with it is that I don't undertsand how to use it with seleium
from selenium import webdriver
import autoit
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("http://na58.evony.com/s.html?loginid=747970653D74727947616D65&adv=index")
driver.maximize_window()
assert "Evony - Free forever " in driver.title
So far I have this and It's doing what is suppose to do which is create a new account using that "driver.get" but when I reach to the page, it is all flash and I can not interact with anything in the webpage so I have to use AutoIT but I don't know how to get it to "pick-up" from where selenium left off. I want it to interact with a button on the webpage and from viewing a previous post on stackoverflow I can use a (x,y) to specify the location but unfortunately that post didn't explain beyond that. Any and all information would be great, thanks.
Yes, you can use any number of scraping libraries (scrapy and beautiful soup are both easy to use and very powerful). Personally though, I like Selenium and its python bindings because they're the most flexible. Your final script would look something like this:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("http://xx.yy.zz")
# Click the "New comer, click here to play!" button
elem = driver. find_element_by_partial_link_text("Click here to play")
elem.send_keys(Keys.RETURN)
Can you post what the source of the page looks like (maybe using a Pastebin)?
Edit: updated to show you how to click the "Click here to play" button.

Categories

Resources