How to add repetitve responses to a webpage using selenium? - python

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)

Related

Selenium can not find element from workera.ai

I am trying to scrape question answers from workera.ai but I am stuck because Selenium cannot find any element I searched for using class. When I check the page source the element is available but Selenium can not find it. Here is what I am doing.
Signup using: https://workera.ai/candidates/signup
from selenium import webdriver
from selenium.webdriver.chrome import service
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
import time, os
option = webdriver.ChromeOptions()
option.add_argument("start-maximized")
option.add_experimental_option("excludeSwitches", ["enable-automation"])
option.add_experimental_option('useAutomationExtension', False)
option.add_argument("--disable-blink-features")
option.add_argument("--disable-gpu")
option.add_argument(r"--user-data-dir=C:\Users\user_name\AppData\Local\Google\Chrome\User Data") #e.g. C:\Users\You\AppData\Local\Google\Chrome\User Data
option.add_argument(r'--profile-directory=Profile 2') # using profile which is logged into the website
#option.add_argument("--headless")
option.add_argument('--disable-blink-features=AutomationControlled')
wd = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=option)
skill_sets = ['https://workera.ai/app/learner/skillset/82746bf6-4eb2-4065-b2fb-740bc3207d14','https://workera.ai/app/learner/skillset/7553e8f8-52bf-4136-a4ea-6aa63eb963d9','https://workera.ai/app/learner/skillset/e11cb698-38c1-4a4f-aa7b-43b85bdf5a51','https://workera.ai/app/learner/skillset/a999048c-ab99-4576-b849-4e72c9455418','https://workera.ai/app/learner/skillset/7df84ad9-ae67-4faf-a981-a95c1c02adbb', 'https://workera.ai/app/learner/skillset/737fa250-8c66-4ea0-810b-6847c304aa5b','https://workera.ai/app/learner/skillset/ed4f2f1f-2333-4b28-b36a-c7f736da9647','https://workera.ai/app/learner/skillset/323ba5d9-fffe-48c0-b7b4-966d1ebca99a','https://workera.ai/app/learner/skillset/488492e9-53c4-4600-b336-6dfe44340402']
# AI fluent AI literate DATA ANAlyst DATA Engineer DATA scientist Deep learn ML Responsible AI Software Engineer
for skill in skill_sets:
wd.get(skill)
time.sleep(20)
num = wd.find_element(By.CLASS_NAME, "sc-jNHgKk hrMhpT")# class name is different for every account
num = num.split('of')[1]
num = int(num)
print(num)
button = wd.find_elements(By.CLASS_NAME, "styled__SBase-sc-cmjz60-0 styled__SPrimary-sc-cmjz60-1 kSmXiJ hwoYMb sc-fKVqWL eOjNfz")
print(len(button))
wd.close()
I don't know why it is happening. Does the site block Selenium web drivers or it is something else?
Edit
I tried getting page source from Selenium and then accessing elements using bs4 and it is working. So I think the website is blocking Selenium by some mean.
The problem with selenium is that you can't select elements that has more than one class like this.
In order to select them, you can either mention one class in the value, or use "."
for example:
wd.find_element(By.CLASS_NAME,"class1.class2")
Also you can select the class that exists for all the answers which I believe it is this one "sc-jNHgKk", so you won't have the problem to select a class for each account, or you can just use XPATH instead.
num = int(wd.find_element(By.CLASS_NAME, "sc-jNHgKk").text.split("of ")[1])
button = wd.find_elements(By.CLASS_NAME, "styled__SBase-sc-cmjz60-0")
print(len(button))

how can i print instagram reply message in python using selenium

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
import time
import schedule
browser = webdriver.Chrome('/Users/badernm/Desktop/chromedriver')
browser.get('https://www.instagram.com/accounts/password/reset/')
usrname_bar = browser.find_element_by_name('cppEmailOrUsername')
username = '17pii_'
usrname_bar.send_keys(username + Keys.ENTER
i need to print the result that is ("thanks! check your email for reset link ")
or ("no account with this email")
I re-created your program and added 4 extra lines that grab that text from the pop-up you want. Then it prints the text to your terminal. I also took out the 'schedule' library because I saw that you weren't using it in your code, but you can add it back in if you're just showing us a snippet of your program.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
import time
browser = webdriver.Chrome('/Users/badernm/Desktop/chromedriver')
browser.get('https://www.instagram.com/accounts/password/reset/')
usrname_bar = browser.find_element_by_name('cppEmailOrUsername')
username = '17pii_'
usrname_bar.send_keys(username + Keys.ENTER)
time.sleep(1) # Wait for pop-up message
result = browser.find_element_by_xpath("html/body/div/div/div/div/p") # Path to pop-up
print(result.text) # Get pop-up text
browser.quit() # Quit browser process
You can get the element by class name. It will look something like this:
reply = browser.find_element_by_class_name("CgFia")
print(reply.text)

Select from dropdown Selenium

New to Selenium. I'm trying to get it so that I can enter a name into a search box and then click on the correct name.
I have managed to write some code to go to a website and then enter what I want and press the search button. The problem is that it then shows a list of items so I'd have to click again.
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from webdriver_manager.chrome import ChromeDriverManager
from time import sleep
index = 'AMZN'
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get('https://www.marketscreener.com')
sleep(6)
text_box = driver.find_element_by_css_selector('#autocomplete')
text_box.send_keys(index)
#select.select_by_index(1)
driver.find_element_by_xpath("//*[#id='recherche_menu']/table/tbody/tr[1]/td/button/img").click()
When I enter the name and don't click It presents a dropdown list so I am trying to select the first item from the dropdown as that is usually what I will want.
I attempted this by using the commented out select.select_by_index code line. But it doesn't quite work.
I just tried also using text_box.send_keys(Keys.DOWN, Keys.RETURN) to move down into the dropdown field, but this doesn't work and just returns the same as what I currently get from clicking.
To be clear what I mean is that currently the code will return this:
But I want it to go straight to the Amazon page so it will return this:
Any help appreciated.
Thanks
This page use many JavaScript to listen the action of the mouse and the key.If you didn't focus on the searchbox,the dropdown will be disappeared.(and you couldn't find it in the source code).
Try to use ActionChains,this works fine on my PC:
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep
index = 'AMZN'
driver = webdriver.Chrome()
driver.get('https://www.marketscreener.com')
action = ActionChains(driver)
sleep(4)
text_box = driver.find_element_by_css_selector('#autocomplete')
action.move_to_element(text_box)
action.click(text_box)
action.send_keys(index)
action.perform()
sleep(1)
driver.find_element_by_xpath('//*[#id="AC_tRes"]/li[1]').click()
If the result couldn't show you,maybe you need to increase the time of sleep().

How to scroll down a twitter page to load next pages and extract the data

I am trying to scroll down comments on a twitter status,trying to extract the page with all the comments(or at least first 5 pages). Using selenium driver for it , but not successful with the scrolling part, so i have to do manually and extract. I am using python 3.6.5 Pls help...
for eg for this tweet - https://twitter.com/TeamYouTube/status/1012415985184206848
Can anyone help me with code..
My code:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Chrome(executable_path="...../chromedriver")
driver.get('https://twitter.com/TeamYouTube/status/1012415985184206848')
for i in range(1,10):
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(3)
ip = input("Enter y to proceed: ")
if(ip == 'y'):
page = driver.page_source
filename = input('Enter file name : ')
path = 'D:/page_'+filename+'.html'
f = open(path,'w',encoding='utf-8')
f.write(page)
f.close()
driver.close()
Try this:
driver.execute_script("arguments[0].scrollTo(0, document.body.scrollHeight);", driver.findElement(By.id("#permalink-overlay-dialog")));
Explanation: you have to scroll a particular div. To be able to do it, you have to find this element on the page and the scroll to the end of the page only this element.
Second suggestion is to use:
from selenium.webdriver.common.keys import Keys
# locate element and simulate 'END' button press
driver.find_element_by_id("permalink-overlay-dialog").send_keys(Keys.END)
if ot won't work try also to extend with ActionChains:
from selenium.webdriver.common.action_chains import ActionChains
element = driver.find_element_by_id("permalink-overlay-dialog")
action = ActionChains(driver)
action.move_to_element(element).perform()
element.send_keys(Keys.END)

Python: Selenium send_key not working

I am trying to use Selenium in Python, as as I am a beginner in doing so I cannot get send_key to work, but most probably it is straight forward and I am missting something.
Here is an example of what I have done so far:
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("https://semantria.com/demo")
item = driver.find_element_by_id("analyze_url_form")
item.send_keys("http://finance.yahoo.com/news/skystar-bio-pharmaceutical-company-provides-133000048.html")
go_button = driver.find_element_by_id("analyze_url_button")
go_button.click()
The idea is that in the https://semantria.com/demo website, there is an empty space that one can enter a website link, and then click on the Go button.
However, it looks like my code does not do this.
Am I doing something wrong? Does this website do something that I should be aware of and change my code accordingly?
Any help on this is really appreciated.
The problem is that you are sending keys to the form element, not the input element inside.
Plus, you can just send the URL with a new line at the end which is the same as you've entered the URL and pressed ENTER key which results in the form being submitted. Works for me:
item = driver.find_element_by_css_selector("form#analyze_url_form input[name=link]")
item.send_keys("http://finance.yahoo.com/news/skystar-bio-pharmaceutical-company-provides-133000048.html" + "\n")
As a bonus, here is how you can grab the sentiment value (you have to let selenium know what to wait for via WebDriverWait and Expected Conditions):
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox()
driver.get("https://semantria.com/demo")
item = driver.find_element_by_css_selector("form#analyze_url_form input[name=link]")
item.send_keys("http://finance.yahoo.com/news/skystar-bio-pharmaceutical-company-provides-133000048.html" + "\n")
wait = WebDriverWait(driver, 30)
sentiment_value = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "strong.sentiment_score_value")))
print(sentiment_value.text)
Prints positive (+0.230).

Categories