0

I am new to Python and to Selenium and was running into a problem using this simple program:

from selenium import webdriver

driver = webdriver.Chrome(executable_path="C:/Users/Marc/Desktop/Chromedriver/chromedriver.exe")

#open google
driver.get("https://www.google.de")

#search for test
driver.find_elements_by_name("q").send_keys("test")
driver.find_elements_by_name("btnK").click()

It just should open the google webpage in Chrome and initiate a search for the word "test". However when opening the webpage a new element pops up informing about data security and cookies and the search is not executed -> "AttributeError: 'list' object has no attribute 'send_key". (As far as I understand it the reason for the pop up is due to the fact that the browser run by chromedriver doesn't use any of the preexisting cookies.)

How can I solve this? I already tried to us the switch_to_alert method but it didn't work out.

edit: For me it is not about the google website itself but how to deal with it in general as it could occur on other websites as well. Why can't I perform an action on this element? or how can I?

Thanks for your help in advance!

Marc
  • 1
  • 2
  • Why not just request the URL `https://www.google.de/search?q=test`? – mkrieger1 Sep 23 '20 at 13:34
  • Does this answer your question? [What are the alternatives now that the Google web search API has been deprecated?](https://stackoverflow.com/questions/4082966/what-are-the-alternatives-now-that-the-google-web-search-api-has-been-deprecated) – mkrieger1 Sep 23 '20 at 13:35
  • This would work. I just tried it out. (Although the pop up shows there as well, but at least the search task is already done) But for me it is less about the google website itself, but more about how to deal with this kind of problem in general as it could occur with other websites as well. Why does the switch_to_alert method doesn't work? Also tried just target the accept button by the class name and send a click - didnt work either. – Marc Sep 23 '20 at 15:45

1 Answers1

0

The popup on the Google website is in a iframe if you check in the development tool in your browser. So, you just need to switch to the context of the iframe like this :

from selenium import webdriver

driver = webdriver.Chrome(executable_path="C:/Users/Marc/Desktop/Chromedriver/chromedriver.exe")

#open google
driver.get("https://www.google.de")

#search for test
iframe = driver.find_element_by_tag_name("iframe")
driver.switch_to.frame(iframe)
elt = driver.find_elements_by_class_name("CwaK9")
elt[2].click()
driver.find_elements_by_name("q")[0].send_keys("test")
driver.find_elements_by_name("btnK").click()
Pierre-Loic
  • 1,524
  • 1
  • 6
  • 12
  • Hey Pierre, thank you for your response. I tried the modified code and it didn't solve the problem either. In the first try I got this error message: "elt[2].click(); IndexError: list index out of range" so I changed to elt.click() without the brackets. Then I got: "elt.click() AttributeError: 'list' object has no attribute 'click'". Do you have any idea how to solve this? – Marc Sep 23 '20 at 22:04
  • If you have the error IndexError, it is maybe because you don't have the same number of elements with the class name "CwaK9". Try with elt[1].click() or elt[0].click(). Otherwise, check the class name of the element in the development tool of your browser – Pierre-Loic Sep 24 '20 at 06:52
  • Okay know the program shows some unexpected/varying behaviour when using the same code. I played around with the indexnumbers behind elt (changed to 1 and 0), got Listindex or AttributeErrer then changed back to 2 and suddenly it worked in the sense it closed the popup but didn't continue to type in the word test. Then I played around with the Index behind q - got errors - got back to q - suddenly it typed in the text refused to push the search button - AttributeError. I tried several times in a row and got varying results - where does this inconsistency come from? – Marc Sep 24 '20 at 09:32
  • Could the problem with the inconsistency be induced by latency due loading times of the response of the website? So that built in delays between the steps would "stabilize" the output. – Marc Sep 24 '20 at 11:56
  • I think that you are right because when I use it on my computer it works all the time. You can add absolute delay of 4 or 5 seconds or a relative delay linked to the full loading of the page – Pierre-Loic Sep 24 '20 at 12:40
  • I tried it with a delay after ```elt[2].click()``` but then I always get list object has no attribute "send_keys" .... . I checked with the development tool - find by name "q" should be correct...; I don't see why it is not working. (btw. How do I implement an relative delay in respect to the full loading of a page?) – Marc Sep 26 '20 at 18:19