我的代码为什么无法检测到Selenium中的元素?

huangapple go评论52阅读模式
英文:

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表达式将定位到"搜索"元素(如下所示):

英文:

Are you trying to click the below 'Search' button?

我的代码为什么无法检测到Selenium中的元素?

If yes, change your XPath expression to below:

//div[contains(@class,'_aacl _aacp _aacu _aacx _aada') and contains(text(),'Search')]

This XPath expression will locate the Search element(see below):

我的代码为什么无法检测到Selenium中的元素?

huangapple
  • 本文由 发表于 2023年2月19日 14:26:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75498376.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定