即使有匹配也没有点击



代码

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()

终端

PS C:UsersmrmadDesktopselenium test> & C:/Users/mrmad/AppData/Local/Programs/Python/Python310/python.exe "c:/Users/mrmad/Desktop/selenium test/google.py"
c:UsersmrmadDesktopselenium testgoogle.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)

我得到了myi搜索的打印列表你可以看到终端打印列表中有myip,所以它应该与我的if条件匹配,它会被点击,所以我应该在谷歌上搜索myip,不知怎么的我没有得到它还有我打印了很长的列表,这里是1,但我认为不应该是,你可以看到我得到10个搜索结果,所以应该是10个

使用

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

我看到你的XPATH是错误的,你目前正在做的是在你的页面中获取所有的ul标签。然而,您在这里真正需要做的是获取所有li元素,因为您所有的自动完成建议都存储在li中。

虽然您的XPATH字符串可以进行优化,但我会通过简单的更改来保持它的原样。

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")

现在应该可以了。

您。。可能不需要很长的xpath来获取这些元素。这里有一个(稍微(更强大的解决方案,如果出现cookie按钮,它将取消cookie按钮,等待元素加载,搜索字符串,浏览结果并单击相关的:

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

selenium/chrome设置是针对linux的,但是相关部分是:

  • 导入:WebDriverWait,expected_conditions,By
  • browser.get(url(之后的部分,在这里我们明确地等待元素加载并可点击

相关内容

  • 没有找到相关文章

最新更新