简体   繁体   English

似乎在 python 中找不到具有 selenium 的元素

[英]Can't seem to find element with selenium in python

I'm a new coder, and i've recently gotten into web scraping and automation.我是一名新的编码员,我最近进入了 web 抓取和自动化。 I'm trying to make a program that will Log In on a trading platform, and search for the stock Facebook "FB"我正在尝试制作一个在交易平台上登录的程序,并搜索股票 Facebook "FB"

The Log In part seems to be working fine, but the searching for FB stock isn't.登录部分似乎工作正常,但搜索 FB 股票却不行。 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.我已经尝试了所有我知道的 (find_element_by) 方法,但它仍然无法正常工作。

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('kebababdulaziz@gmail.com')

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!提前非常感谢!

Use this:用这个:

search_elem = browser.find_element_by_class_name('market-search__button)

Instead of using the full class name with spaces between them...而不是使用完整的 class 名称,它们之间有空格......

You have target wrong element.You should target input element.您的目标元素错误。您应该定位input元素。

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.为了避免sleep()不必要地减慢你的脚本,最好的做法是 Induce WebDriverWait () 并等待element_to_be_clickable () 并遵循 css 选择器。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM