how to press enter in selenium python? - python-3.x

I want to search a special keyword in Instagram. For example, I want to search this word:"fast food". I can send this key in search box. But, when I use submit method in Selenium by Python3, it doesn't work and give me error. This is my code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.alert import Alert
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
import time
driver = webdriver.Firefox()
url="https://www.instagram.com/p/pTPI-kyX7g/?tagged=resurant"
driver.get(url)
#login_button=driver.find_element_by_xpath("""/html/body/span/section/main/article/div[2]/div[2]/p/a""")
#login_button.click()
import time
driver.implicitly_wait(5)
search_button=driver.find_element_by_xpath("""/html/body/span/section/nav/div[2]/div/div/div[2]/input""")
search_button.send_keys("fast food")
search_button.submit()
This is gave error:
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: ./ancestor-or-self::form
Could you help me?

Instead of. submit() try '.send_keys(u'\ue007')'
See: http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.common.keys

It need more clicks:
search_button=driver.find_element_by_xpath("""/html/body/span/section/nav/div[2]/div/div/div[2]/input""")
search_button.send_keys("fast")
while True:
search_button.send_keys(u'\ue007')

That is very interesting. Another solution is better. It is very important to know that, you must press 2 Enter press on Instagram search box. So, for eliminating while loop, you can use this code:
search_button.send_keys("#fast")
time.sleep(5)
search_button.send_keys(u'\ue007')
search_button.send_keys(u'\ue007')

Related

How find an specific element of page in Selenium python, because I can't find them

I have tried all ways that I knew, but can't found elements "login" and "password". Could someone help me, please? I am tired...
link: https://trading.finam.ru/
from selenium import webdriver
import traceback
from time import sleep
url = 'https://trading.finam.ru/'
driver = webdriver.Chrome(executable_path=r"C:\Users\Idensas\PycharmProjects\pythonProject\selenium\chromedriver.exe")
driver.get(url)
sleep(2)
try:
x = driver.find_element_by_xpath('//label[contains(text(),"Логин")]')
x.send_keys('123')
except Exception:
traceback.print_exc()
finally:
driver.close()
driver.quit()
The picture of that site
The following xpaths will work:
//input[#name='login']
//input[#name='password']
I see the site is loaded slowly so you must put some delay, preferably explicit wait there like:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait.until(EC.visibility_of_element_located((By.XPATH, "//input[#name='login']")))
before trying to insert a texts there
Any website takes sometime to completely load and all the elements are visible to user.
To check the time take taken by your website in different regions you run a check on https://gtmetrix.com/
You can use the simple sleep for few secs until the page is loaded
from selenium import webdriver
import time
driver=webdriver.Chrome(//chromedriver_path_in_your_local//)
driver.get("https://trading.finam.ru/")
time.sleep(5)
login=driver.find_element_by_xpath("//input[#name='login']")
password=driver.find_element_by_xpath("//input[#name='password']")

Python Selenium can't sendkeys (can't find input)

I'm new to Python selenium and today I'm stuck with an element, I don't know why but I can't find the element and sendkeys. Can anyone please explain for me why i can't find it by id / xpath / css selector ? and Also how can we find it and sendkeys ?
Here is a screenshot of the element:
and the website is : https://www.messenger.com/
PS: I've tried java_execute_script but it doesn't work, I still don't know how to use it well.
I'm use python to write selenium program. I think you can press a key F12 and check the html code.
I help you to checked the class name.
I checked the class name is _1mf , but I'm not sure what is your class name. You can check it.
enter image description here
And then, you need to use the following code to finish it.
k=driver.find_element_by_class_name("_1mf")
k.send_keys('testing')
from selenium.webdriver.common.keys import Keys
k.send_keys(Keys.RETURN)
'testing' is my input element. You can change it.
'k' is my variable. You can change it.
I'm not sure is this a correct answer.
You can also reference the following videos:
https://www.largitdata.com/course/105/
https://www.largitdata.com/course/104/
Please find below solution
inputBox = wait.until(EC.element_to_be_clickable((By.XPATH, "//div[#class='_kmc _7kpg navigationFocus']")))
actionChains = ActionChains(driver)
actionChains.move_to_element(inputBox).send_keys("Username").perform()
Note: Please add below import to your solution
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains

Failing to click on print (image element) on a webpage using selenium (python)

I am trying to click on the print option on this webpage:
(http://agence-prd.ansm.sante.fr/php/ecodex/frames.php?specid=65123812&typedoc=R&ref=R0306505.htm)
I am using the code below:
link ='http://agence-prd.ansm.sante.fr/php/ecodex/images/icoimp.gif'
image_elements = driver.find_element_by_xpath("//div[#align='center']/a/img[#src='"+link+"']")
image_element.click()
I have tried many different options to find the element but I get error :
Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[#align='center']/a/img[#src='http://agence-prd.ansm.sante.fr/php/ecodex/images/icoimp.gif']"}
I think I am missing out on something. It'll be great if someone can help me here.
Thanks
This element is present within a iframe please refer below solution to solve your issue:
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.webdriver.common.action_chains import ActionChains
driver = webdriver.Chrome(executable_path=r"../driver/chromedriver.exe")
driver.maximize_window()
driver.get("http://agence-prd.ansm.sante.fr/php/ecodex/frames.php?specid=65123812&typedoc=R&ref=R0306505.htm")
iframe=WebDriverWait(driver,10).until(EC.presence_of_element_located((By.NAME,"left")))
driver.switch_to.frame(iframe)
printButton=WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,"//body[#class='menu']//div//a//img")))
printButton.click()
Have you tried:
image_elements = driver.find_element_by_xpath("/html/body/div/a/img")
image_element.click()

Retrieving elements with custom HTML attributes

I have the following website: https://www.kvk.nl/handelsregister/publicaties/, where I would like to retrieve the login link with Selenium, Scrapy and Python. So for the relevant function, I have the following code:
def start_requests(self):
self.driver = webdriver.Chrome(executable_path=os.path.join(os.getcwd(), "Drivers", "chromedriver.exe"))
self.driver.get(self.initial_url)
test = access_page_wait.until(expected_conditions.visibility_of_element_located((By.CSS_SELECTOR, 'a[data-ui-test-class="linkCard_toegangscode"]')))
if test.is_displayed():
print("+1")
else:
print("-1")
However, this does not seem to work, since it just waits 15 seconds and then it stops. It will never reach +1 or -1.
Now my question is, how can we point selenium to the correct element. It also does not seem to work using XPATH find_elements_by_xpath("//a[#data-ui-test-class='linkCard_toegangscode']").
Should I use another selection approach and if yes, which one?
Because there is Frame which stopping you to access the element.Switch_To iframe and then access the element.
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
import os
driver = webdriver.Chrome(executable_path=os.path.join(os.getcwd(), "Drivers", "chromedriver.exe"))
driver.get("https://www.kvk.nl/handelsregister/publicaties/")
driver.switch_to.frame(0)
test=WebDriverWait(driver,10).until(expected_conditions.visibility_of_element_located((By.CSS_SELECTOR, 'a[data-ui-test-class="linkCard_toegangscode"]')))
if test.is_displayed():
print("+1")
else:
print("-1")
Try the above code.It should print what you are looking after.

how to press enter in selenium python with out any error?

I want to search a special keyword in Instagram. For example, I want to search this word:"internet". I can send this key in search box. But, when I use submit method in Selenium by Python3, it doesn't work and give me error. This is my code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.alert import Alert
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
import time
driver = webdriver.Firefox()
url="https://www.instagram.com/p/pTPI-kyX7g/?tagged=resurant"
driver.get(url)
#login_button=driver.find_element_by_xpath("""/html/body/span/section/main/article/div[2]/div[2]/p/a""")
#login_button.click()
import time
driver.implicitly_wait(5)
search_button=driver.find_element_by_xpath("""/html/body/span/section/nav/div[2]/div/div/div[2]/input""")
search_button.send_keys("internet",Keys.RETURN)
This is no anu error, but it dosen't work.
By this code enter will be pressed, but, after pressing and loading page an error will be occurred:
search_button.send_keys("internet")
while True:
search_button.send_keys(u'\ue007')
Have you tried
searchbox driver.find_element_by_xpath( Xpath/locator for search box).sendKey("internet")
search button driver.find_element_by_xpath(Xpath/locator for the search key button).click
which is you send the "internet" to that edit Box first then looking for that search button and perform .click
not input the search text and send the key at sametime
Use this solution please:
time.sleep(5)
search_button.send_keys(u'\ue007')
search_button.send_keys(u'\ue007')

Resources