2

I want to make python crawling. I already logged on this website and want to send_keys(keyword) and click the button. I try to find css selector or xpath but there is some error as follows.

from selenium import webdriver as wd
import time

main_url2 = 'https://manage.searchad.naver.com/customers/668860/tool/keyword-planner'

driver.find_element_by_xpath('//*[@id="wrap"]/div/div/div[1]/div[1]/div/div/div/div[2]/div[1]/div[1]/div[2]/form/div[1]/div/div/textarea').send_keys(keyword)

keyword ='모기퇴치기'

This is the error messasge as follows

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="wrap"]/div/div/div1/div1/div/div/div/div[2]/div1/div1/div[2]/form/div1/div/div/textarea"} (Session info: chrome=68.0.3440.106) (Driver info: chromedriver=2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e),platform=Windows NT 10.0.16299 x86_64)

This is the image of website and resources of chrome driver.

enter image description here

문정아
  • 141
  • 11
  • Did you type that xpath by hand, or did you right-click the element in the console and copy the xpath from there? – John Gordon Aug 19 '18 at 15:25
  • And is that element present on the page when it first loads, or does the page have javascript that dynamically adds that element based on user activity? – John Gordon Aug 19 '18 at 15:27
  • I did the right-click the element in the console and copy it from there. – 문정아 Aug 19 '18 at 15:27
  • I studied coding for 2 weeks so I am not sure. I think that that page has javascript that dynamically adds that element based on user activity. Third menu '도구' on top area and scroll down and click '키워드도구', and this page showed – 문정아 Aug 19 '18 at 15:35
  • In that case, your script will also have to perform those same actions to make that element available. – John Gordon Aug 19 '18 at 15:40
  • Thank you for your comment. Actually there was Popup window and I thought that I cannot handle it, so I pushed URL directly. I think I need to solve the popup window problem first. – 문정아 Aug 19 '18 at 15:48
  • @문정아 Do you have a set of demo credentials for us to access the desired elements? – undetected Selenium Aug 20 '18 at 04:59

1 Answers1

0

I had the similar issue but it was a race condition. the driver has to wait for the particular event to show up in the page before accessing or sending keys.

try this code

try{
     Thread.Sleep(5000);
 }catch(Expression ex){
     system.out.print(ex.getmessage());
 }

before sendkeys

or just put Thread.sleep(5000); at first

Note: 5000 as in 5 seconds or 5000 milliseconds try a larger number at first like 10000 milliseconds, then reduce the time.

PRagh
  • 92
  • 5
  • 1
    Import Thread Thread.sleep(5000); ModuleNotFoundError: No module named 'Thread' I tried to do it, but there is a No module error. – 문정아 Aug 20 '18 at 15:06
  • 1
    That was in Java perspective. in the case of Python, I think it is - use this import statement `import time` - use this before sendkeys `time.sleep(seconds)` – PRagh Aug 20 '18 at 15:11
  • Thank you for your comment. It works~!! I tried to solve this matter for few hours and you saved my time. Thank you again. – 문정아 Aug 20 '18 at 15:28
  • you're welcome. This solution is temporary, there are effective ways which are better than sleep function (because it is not recommended). I suggest you refer to this [link](https://selenium-python.readthedocs.io/waits.html) to use such waits in Python. If it is Java I have a better solution than using Thread.sleep. – PRagh Aug 20 '18 at 15:36