Home > Software design >  not getting click even there is match
not getting click even there is match

Time:07-31

code

from http.server import executable
from multiprocessing.connection import wait
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time

driver = webdriver.Chrome(executable_path="C:\\Program Files (x86)\\chromedriver.exe") 


driver.get("https://www.google.com")
driver.implicitly_wait(5)
driver.find_element(By.NAME,"q").send_keys('myi')
print(driver.title)

optionsList = driver.find_elements(By.XPATH,"//body/div[@class='L3eUgb']//form[@role='search']//div[@class='A8SBwf emcav']//div[@class='mkHrUc']/ul[1]/div[@role='presentation']/ul[@role='listbox']")
print(len(optionsList))

for searchtxt in optionsList:
    print(searchtxt.text)
    
    if searchtxt.text == 'myip':
        searchtxt.click()
        break

time.sleep(25)

driver.quit()

terminal

PS C:\Users\mrmad\Desktop\selenium test> & C:/Users/mrmad/AppData/Local/Programs/Python/Python310/python.exe "c:/Users/mrmad/Desktop/selenium test/google.py"
c:\Users\mrmad\Desktop\selenium test\google.py:11: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
  driver = webdriver.Chrome(executable_path="C:\\Program Files (x86)\\chromedriver.exe")

DevTools listening on ws://127.0.0.1:55485/devtools/browser/87772c6f-7fa4-4186-81e6-7f1964cb27d4
Google
1
myip
myims
myitreturn
myims login
myinterview practice.com
myiasis
myimaginestore
myinfo lakehead
myillini
myimmitracker
[6992:11244:0730/224205.248:ERROR:device_event_log_impl.cc(214)] [22:42:05.247] USB: usb_device_handle_win.cc:1048 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)
[6992:11244:0730/224205.249:ERROR:device_event_log_impl.cc(214)] [22:42:05.249] USB: usb_device_handle_win.cc:1048 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)

im getting print list of search of myi you can see there is myip in terminal printed list so it should be match with my if condition and it will be click so i should get myip searched on google somehow im not getting it and also there is i have print lenth of list here it is 1 but i think it shouldn't be you can see im getting 10 search result so it should be 10

using

chrome - 103.0.5060.134
chromewebdriver - 103.0.5060.134
python- 3.10.5
selenium - 4.3.0

CodePudding user response:

I see your XPATH is wrong, what you are doing currently is get all the ul tags in your page. However what you really need to do here is to get all li elements since all your autocomplete suggestions are stored in li.

Although your XPATH string can be optimized, but I would keep it the way you have with a simple change.

optionsList = driver.find_elements(By.XPATH,"//body/div[@class='L3eUgb']//form[@role='search']//div[@class='A8SBwf emcav']//div[@class='mkHrUc']/ul[1]/div[@role='presentation']/ul[@role='listbox']/li")

Now this should work.

CodePudding user response:

You .. probably don't need long xpaths to get those elements. Here is a (slightly) more robust solution, which will dismiss the cookie button if it comes up, wait for the elements to load, search for your string, go through the results and click on the relevant one:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains

chrome_options = Options()
chrome_options.add_argument("--no-sandbox")


webdriver_service = Service("chromedriver/chromedriver") ## path to where you saved chromedriver binary
browser = webdriver.Chrome(service=webdriver_service, options=chrome_options)
url = 'https://www.google.com'
browser.get(url)
try:
    deny_cookies = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@id='W0wltc']")))
    deny_cookies.click()
except Exception as e:
    print('no cookies')
search_box = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='q']")))
search_box.send_keys('myi')
search_box.click()
options = WebDriverWait(browser, 30).until(EC.presence_of_all_elements_located((By.XPATH, "//li[@data-view-type='1']")))
for option in options:
    print(option.text)
    if option.text == 'myip':
        option.click()
        break

The selenium/chrome setup is for linux, however the relevant parts are:

  • imports: WebDriverWait, expected_conditions, By
  • the part after browser.get(url), where we explicitly wait for the elements to load and be clickable
  • Related