英文:
Why isn't my code detecting elements in selenium?
问题
以下是您要的代码部分的中文翻译:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
Username = ""
Password = ""
driver = webdriver.Chrome()
driver.maximize_window()
driver.get(url="https://www.instagram.com/")
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="loginForm"]/div/div[1]/div/label/input'))).send_keys(f"{Username}")
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="loginForm"]/div/div[2]/div/label/input'))).send_keys(f"{Password}")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="loginForm"]/div/div[3]/button/div'))).click()
//Code stops working after this
WebDriverWait(driver, 40).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="mount_0_0_YP"]/div/div/div/div[1]/div/div/div/div[1]/div[1]/div[1]/div/div/div/div/div[2]/div[2]/div/a/div'))).click()
print("Pass")
希望这有所帮助!如果您有任何其他问题,欢迎提问。
英文:
I'm trying to automate Instagram. My code works when logging in, after that, it detects nothing. All I'm trying to do is hit the search button, and it wont detect it. I tried finding element by CSS selector, class and xpath and nothing is working.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
Username = ""
Password = ""
driver = webdriver.Chrome()
driver.maximize_window()
driver.get(url="https://www.instagram.com/")
WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.XPATH,'//*[@id="loginForm"]/div/div[1]/div/label/input'))).send_keys(f"{Username}")
WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.XPATH,'//*[@id="loginForm"]/div/div[2]/div/label/input'))).send_keys(f"{Password}")
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="loginForm"]/div/div[3]/button/div'))).click()
//Code stops working after this
WebDriverWait(driver,40).until(EC.element_to_be_clickable((By.XPATH,'_//*[@id="mount_0_0_YP"]/div/div/div/div[1]/div/div/div/div[1]/div[1]/div[1]/div/div/div/div/div[2]/div[2]/div/a/div'))).click()
print("Pass")
答案1
得分: 1
你是否要点击下面的"搜索"按钮?
如果是的话,请将你的XPath表达式更改为以下内容:
//div[contains(@class,'_aacl _aacp _aacu _aacx _aada') and contains(text(),'Search')]
这个XPath表达式将定位到"搜索"元素(如下所示):
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论