Reputation: 59
I'm a new coder, and i've recently gotten into web scraping and automation. I'm trying to make a program that will Log In on a trading platform, and search for the stock Facebook "FB"
The Log In part seems to be working fine, but the searching for FB stock isn't. The automation process won't type anything in the search bar. I i've tried all the (find_element_by) methods that i know of, but it still wont work.
The account details will be in the program, if you want to run the code yourself "FEEL FREE!" it's a fake account...
Here is my code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
browser = webdriver.Chrome('/Users/larskvist/downloads/chromedriver')
browser.get('https://www.forex.com/en-uk/account-login/')
username_elem = browser.find_element_by_name('Username')
username_elem.send_keys('[email protected]')
time.sleep(0.5)
password_elem = browser.find_element_by_name('Password')
password_elem.send_keys('KEbababdulaziz')
password_elem.send_keys(Keys.ENTER)
time.sleep(5)
search_elem = browser.find_element_by_class_name('market-search__button ng-pristine ng-valid ng-touched')
search_elem.send_keys('FB')
time.sleep(30)
browser.close()
This is the element i wan't to get:
<input _ngcontent-c23="" appcustomplaceholder="" class="market-search__search-input ng-pristine ng-valid ng-touched" formcontrolname="query" type="text" placeholder="Search markets">
Thanks a lot in advance!
Upvotes: 1
Views: 169
Reputation: 33384
You have target wrong element.You should target input
element.
To avoid sleep()
which unnecessary slow down your script it is best practice to Induce WebDriverWait
() and wait for element_to_be_clickable
() and following css selector.
search_elem = WebDriverWait(browser,20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"input.market-search__search-input")))
search_elem.send_keys('FB')
You need to import below libraries.
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
Upvotes: 1
Reputation: 1099
Use this :
search_elem = browser.find_element_by_class_name('market-search__button)
Instead of using the full class name with spaces between them...
Upvotes: 1