Hi I'm new to selenium but trying to automate searching on angel.co without success! The search box element changes when clicked on and I'm having difficuly with selenium being able to pick up the elements to do things on. Essentially I want to input growth hacker into the box and do a search. You have to first click on the box, type in the search term and then press enter.
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver.get("https://angel.co/jobs")
time.sleep(5)
WebDriverWait(driver,20).until(EC.visibility_of_element_located
((By.XPATH,"//[#id='main']/div/div[5]/div[2]/div/div[2]/div[1]/div[1]/
button/div")))
element = driver.find_element_by_xpath('//*[#id="main"]/div/div[5]/div[2]/div/div[2]/div[1]/div[1]/button/div')
driver.execute_script("arguments[0].innerText = 'Growth Hacker';",element)
time.sleep(5)
element.send_keys(Keys.RETURN')
I get as far as being able to input the Growth Hacker into the box, but can't make it become a tag as all search terms do in this search box.
Any help would be really appreciated.
UPDATE
I've managed to insert text into the box but for some reason I can't seem to get it to turn into the tag with the correct text on the search box.
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver.get("https://angel.co/jobs")
WebDriverWait(driver,60).until(EC.element_to_be_clickable((By.XPATH,'//span[#class="label_82eab"]')))
element4 = driver.find_element_by_xpath('//span[#class="label_82eab"]')
time.sleep(5)
driver.execute_script("arguments[0].innerText='Edinburgh';",element4)
time.sleep(5)
WebDriverWait(driver,60).until(EC.element_to_be_clickable((By.XPATH,'//span[#class="label_82eab"]')))
driver.find_element_by_xpath('//span[#class="label_82eab"]').click()
Before Click
After click
Text to Change
So i tried my own way and was able to search inputted result, you can try the code and see if its what you want, because your question wanst clear enough. From my understanding your finding difficulties in searching the text field which works fine in my code. Try it out and let me know if i can help !
You can always write the code without the classes if your not familiar with selenium and classes!
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re
class Angel(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome("change to ur chrome driver")
self.driver.implicitly_wait(30)
def test_angel(self):
driver = self.driver
driver.get("https://angel.co/")
driver.find_element_by_id("search").click()
time.sleep(3)
driver.find_element_by_id("search").clear()
time.sleep(3)
driver.find_element_by_id("search").send_keys("Growth Hacker")
time.sleep(3)
driver.find_element_by_id("search").send_keys(Keys.ENTER)
time.sleep(3)
def tearDown(self):
self.driver.quit()
if __name__ == "__main__":
unittest.main()
Related
I'm trying to automate Instagram. My code works when logging in, after that, it detects nothing. All I'm trying to do is hit the search button, and it wont detect it. I tried finding element by CSS selector, class and xpath and nothing is working.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
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
Username = ""
Password = ""
driver = webdriver.Chrome()
driver.maximize_window()
driver.get(url="https://www.instagram.com/")
WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.XPATH,'//*[#id="loginForm"]/div/div[1]/div/label/input'))).send_keys(f"{Username}")
WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.XPATH,'//*[#id="loginForm"]/div/div[2]/div/label/input'))).send_keys(f"{Password}")
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,'//*[#id="loginForm"]/div/div[3]/button/div'))).click()
//Code stops working after this
WebDriverWait(driver,40).until(EC.element_to_be_clickable((By.XPATH,'_//*[#id="mount_0_0_YP"]/div/div/div/div[1]/div/div/div/div[1]/div[1]/div[1]/div/div/div/div/div[2]/div[2]/div/a/div'))).click()
print("Pass")
Are you trying to click the below 'Search' button?
If yes, change your XPath expression to below:
//div[contains(#class,'_aacl _aacp _aacu _aacx _aada') and contains(text(),'Search')]
This XPath expression will locate the Search element(see below):
Python/coding newb, beware!
Im writing a script to download from youtube from " https://ytmp3.cc/en13/ ". I had written a click script using pyautogui but the problem being that the download button appears anywhere betweeen 1 and 15 seconds of entering the link. So i wanted to re-code it in a Selenium window to dynamically wait until the button is visible and then continue with the click script. I have tried about 15 ways but cant get it to work
library blah :
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support.expected_conditions import presence_of_element_located
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
ways i tried to get it to work:
without waits, results in NosuchElement Exception
def check_exists():
try:
while True:
a=driver.find_element(By.LINK_TEXT, "Download")
a.click()
except NoSuchElementException:
time.sleep(10)
the syntax of this is most probably wrong
i tried a couple different variations of implicit and explicit waits but couldnt get it to work. Is there some javascript on the site linked above thats messing with my code?
in a separate .py, running
#driver.find_element(By.LINK_TEXT, "Download").click()
#either the line above or below works at finding the element, and clicking it
driver.find_element_by_link_text("Download").click()
I just need help getting the line above ^ into an explicit wait, but i dont have the language knowledge of how to do it yet. i would appreciate some help, thanks in advance!
You can use webdriver wait to wait until button is displayed. Tried the below code which works for me
from selenium import webdriver
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.common.by import By
video_path = "your_video_path"
waitTime=10
driver = webdriver.Chrome("./chromedriver.exe")
driver.get("https://ytmp3.cc/")
element = driver.find_element_by_id("input")
element.send_keys(video_path)
driver.find_element_by_id("submit").click()
try:
# wait 10 seconds before looking for element
element = WebDriverWait(driver, waitTime).until(
EC.presence_of_element_located((By.LINK_TEXT,"Download"))
)
driver.find_element_by_link_text("Download").click()
finally:
print(driver.title)
You can update your required wait time in waitTime variable in seconds so if the value is 10 selenium will wait and check for elements upto 10 seconds
I'm having a big problem with this one and I just don't know what to do now.
So, I'm trying to use selenium to get an information on a sort of a pop up.pop up
(This is this exact pop-up, it's on tiktok)
The HTML element of the button followers : Followers<
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
driver = webdriver.Firefox(executable_path='./geckodriver')
driver.get("https://www.tiktok.com/#programm___r?lang=en") # on lance tiktok sur ordi
# first thing first, just click on the button that "launch" the pop up
driver.find_element_by_class_name('jsx-1737374796.header-inbox-icon').click()
# Then, I want to click on "Followers" category but this is getting complicated here
# First idea to click on the category, check if it contains the word "followers"
if (driver.find_element_by_xpath('/html/body/div[1]/div/div[1]/div/div[3]/div[2]/div[2]/div/div[1]/div/span[5]').text) == "Followers" : # this line works, no problem
driver.find_element_by_xpath('/html/body/div[1]/div/div[1]/div/div[3]/div[2]/div[2]/div/div[1]/div/span[5]').click() # this one doesn't work i don't know why
# So, second idea, try with the webdriverwait
WebDriverWait(driver,20).until(EC.presence_of_element_located((By.XPATH,'/html/body/div[1]/div/div[1]/div/div[3]/div[2]/div[2]/div/div[1]/div/span[5]'))) # this works
driver.find_element_by_xpath('/html/body/div[1]/div/div[1]/div/div[3]/div[2]/div[2]/div/div[1]/div/span[5]').click() # this still doesn't work
# Third idea, instead o xpath, css-selector
WebDriverWait(driver,20).until(EC.presence_of_element_located((By.CSS_SELECTOR,"span.jsx-2344352055:nth-child(5)"))) # work, no problem
driver.find_element_by_css_selector("span.jsx-2344352055:nth-child(5)").click() # doesn't work neither..
# Fourth and last idea, probably the least fine, get all the elements with the class name, and only one return Followers with the .text, but still the same problem
elements = (driver.find_elements_by_class_name("jsx-2344352055"))
for i in range(len(elements)) :
if elements[i].text == "Followers" :
elements[i].click() # but still doesn't work
Sometimes, it worked, like I don't know why or how but sometimes, the click works, but like 95% of the time, it doesn't and I really don't know why
Thanks in advance for your help !
You should wait for the button to be clickable using WebDriverWait and element_to_be_clickable expected condition. For example:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
driver = webdriver.Firefox(executable_path='./geckodriver')
driver.get("https://www.tiktok.com/#programm___r?lang=en")
WebDriverWait(driver,20).until(EC.presence_of_element_located((By.XPATH,'/html/body/div[1]/div/div[1]/div/div[3]/div[2]/div[2]/div/div[1]/div/span[5]')))
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,'/html/body/div[1]/div/div[1]/div/div[3]/div[2]/div[2]/div/div[1]/div/span[5]'))).click()
Please try this one:
driver = webdriver.Firefox(executable_path='./geckodriver')
driver.get("https://www.tiktok.com/#programm___r?lang=en") # on lance tiktok sur ordi
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS) ;
# first thing first, just click on the button that "launch" the pop up
driver.find_element_by_class_name('jsx-1737374796.header-inbox-icon').click()
I hope this resolves your issue.
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()
Following is the code which im trying to run
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import os
import time
#Create a new firefox session
browser=webdriver.Firefox()
browser.maximize_window()
#navigate to app's homepage
browser.get('http://demo.magentocommerce.com/')
#get searchbox and clear and enter details.
browser.find_element_by_css_selector("a[href='/search']").click()
search=browser.find_element_by_class_name('search-input')
search.click()
time.sleep(5)
search.click()
search.send_keys('phones'+Keys.RETURN)
However, im unable to submit the phones using send_keys.
Am i going wrong somewhere?
Secondly is it possible to always use x-path to locate an element and not rely on id/class/css-selections etc ?
The input element you are interested in has the search_query class name. To make it work without using hardcoded time.sleep() delays, use an Explicit Wait and wait for the search input element to be visible before sending keys to it. Working code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
browser = webdriver.Firefox()
browser.maximize_window()
wait = WebDriverWait(browser, 10)
browser.get('http://demo.magentocommerce.com/')
browser.find_element_by_css_selector("a[href='/search']").click()
search = wait.until(EC.visibility_of_element_located((By.CLASS_NAME, "search-query")))
search.send_keys("phones" + Keys.RETURN)