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
Related
On the following website (https://play2048.co/) is a game called 2048. After I use Selenium to play it eventually gets to a point where is says Game Over!
When I inspect the element (see picture) I see the word Game Over! in p tags. I wish to be able to capture this text and store it in a Python variable as below:
TextV = wait.until(EC.visibility_of_element_located((By.XPATH, '/html/body/div[2]/div[3]/div[1]/p'))).text
I got the XPath by simply right clicking the text in the p tags (inspect element) and selected copy full XPath but it seems Selenium cannot find this text element when I run the whole code.
Thannks
Personally I use CSS_Selector, because it tends to be more correct, and it's more likely to be successful.
So i would recommend something along the lines:
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
# Your Code Here
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR,'the css selector'))).text
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()
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.
So Im trying to click this element:
//*[#id="ember1720"]
But Selenium tells me it can't find it. Im doing the operation like so: driver.find_element_by_xpath('//*[#id="ember1720"]').click()
I've tried the same program on the official python website and it works perfectly there.
First make sure the element has been loaded correctly:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
...
WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.XPATH, """//*[#id="ember1720"]""")))
or maybe:
WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, """//*[#id="ember1720"]""")))
Alternatively, try to find the element by id:
driver.find_element_by_id('ember1720').click()
I've faced issues in the past where I could find the element by its ID but not by xpath.
I'm not sure if that's your exact problem, but depending on IDs like this, which seems to be auto generated are not a good practice.
Please read my blog post for the best practices for choosing the best locator.
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')