[英]How can I put a condition to check if an element has been clicked?
from selenium import webdriver
import time
import pyautogui
web = webdriver.Chrome()
print("trying to run the program")
yeah = web.get("https://www.google.com")
time.sleep(5)
searchbar = web.find_element_by_xpath('/html/body/div[1]/div[3]/form/div[1]/div[1]/div[1]/div/div[2]/input')
if pyautogui.click() == web.find_element_by_xpath('/html/body/div[1]/div[3]/form/div[1]/div[1]/div[1]/div/div[2]/input'):
searchbar.send_keys("yeah! We did it!")
我希望程序檢測用戶是否點擊了搜索欄,然后在搜索欄上發送鍵(例如,以谷歌搜索欄為例)。 如果只有用戶點擊了搜索欄,我想發送密鑰。 我怎樣才能做到這一點? (忽略代碼,這只是為了演示)。
在大多數情況下,您可以檢測到所選元素。 但並非在所有情況下。
有時該元素會更改或添加一些類名,有時會更改某些屬性,有時會更改某些其他元素屬性。
這取決於您正在使用的頁面。
例如,在沒有登錄用戶的 Google 主頁上,您可以執行以下操作:
from selenium import webdriver
import time
import pyautogui
web = webdriver.Chrome()
print("trying to run the program")
yeah = web.get("https://www.google.com")
time.sleep(5)
indicator_el = web.find_element_by_css_selector('div.UUbT9')
indicator_style = indicator_el.get_attribute("style")
if "none" in indicator_style:
print("search bar not clicked")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.