在Python中使用Selenium查找元素

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

Find Elements ın Python Selenium

问题

以下是您的翻译内容:

"Im trying to find some button elements with XPATH,CSS_SELLECTOR since yesterday but my codes is not working. I dont understand I have one of the tags I chose in the website but cant find the element what is the problem ?"

我昨天开始尝试使用XPATH和CSS选择器来查找一些按钮元素,但我的代码不起作用。我不明白,我已经选择了网站中的一个标签,但无法找到元素,问题在哪里?

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time

class Instagram:
    def __init__(self, username, password):
        self.username = username
        self.password = password
        self.browser = webdriver.Chrome()

    def signIn(self):
        self.browser.get("https://www.instagram.com/")
        self.browser.maximize_window()
        time.sleep(2)
        self.browser.find_element(By.XPATH, '//*[@id="loginForm"]/div/div[1]/div/label/input').send_keys(self.username)
        time.sleep(1)
        self.browser.find_element(By.XPATH, '//*[@id="loginForm"]/div/div[2]/div/label/input').send_keys(self.password)
        time.sleep(1)
        self.browser.find_element(By.CSS_SELECTOR, 'button[class="_a9-- _a9_0"]').click()
        time.sleep(5)

    def getFollowers(self):
        self.browser.get(f"https://www.instagram.com/{self.username}/")
        self.browser.find_element(By.XPATH, '//*[@id="mount_0_0_JS"]/div/div/div[1]/div/div/div/div[1]/div[1]/div[2]/section/main/div/header/section/div[1]/div[1]/div[1]/div/a')

# 错误截图:https://i.stack.imgur.com/trbZb.jpg

请注意,我保留了代码部分,并只进行了文本内容的翻译。

英文:

Im trying to find some button elements with XPATH,CSS_SELLECTOR since yesterday but my codes is not working. I dont understand I have one of the tags I chose in the website but cant find the element what is the problem ?

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time
#from instagramuser import username,password

class Instagram:
    def __init__(self,username,password):
        self.username = username 
        self.password = password
        self.browser = webdriver.Chrome()`


    def signIn(self):
        self.browser.get("https://www.instagram.com/")
        self.browser.maximize_window()
        time.sleep(2)
        self.browser.find_element(By.XPATH,'//*[@id="loginForm"]/div/div[1]/div/label/input').send_keys(self.username)
        time.sleep(1)
        self.browser.find_element(By.XPATH,'//*[@id="loginForm"]/div/div[2]/div/label/input').send_keys(self.password)
        time.sleep(1)
        self.browser.find_element(By.CSS_SELECTOR,'button[class="_a9-- _a9_0"]').click()
        time.sleep(5)
        
    def getFollowers(self):
        self.browser.get(f"https://www.instagram.com/{self.username}/")
        self.browser.find_element(By.XPATH,'//*[@id="mount_0_0_JS"]/div/div/div[1]/div/div/div/div[1]/div[1]/div[2]/section/main/div/header/section/div[1]/div[1]/div[1]/div/a')

ERROR PHOTO`

答案1

得分: 1

你的选择器是错误的,这里是正确的XPath:

self.browser.find_element(By.XPATH, '//button[@class="_acan _acap _acas _aj1-"]').click()

请用出现在你的错误中的这个来替换。

另外,一个提示是不要使用time.sleep来等待页面加载,你应该使用WebdriverWait模块,它等待元素最多的一段时间,但当找到它时,程序会立即运行。

示例:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec

element = WebDriverWait(self.browser, 10).until(ec.presence_of_element_located((By.XPATH, '//button[@class="_acan _acap _acas _aj1-"]')))

# 然后你可以执行任何你想要的操作

element.click()

element.send_keys()
英文:

Your selector is wrong, here is the correct Xpath:

self.browser.find_element(By.XPATH, '//button[@class="_acan _acap _acas _aj1-"]').click()

Replace this with the one appearing in your error.

And a tip, don't use time.sleep to wait for page to load, you should use WebdriverWait module, that waits for a maximum period of time for the element to appear, but when it does find it, the program runs instantly.

Example:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec

element = WebDriverWait(self.browser, 10).until(ec.presence_of_element_located((By.XPATH, '//button[@class="_acan _acap _acas _aj1-"]')))

# You can do whatever actions you like then

element.click()

element.send_keys()

huangapple
  • 本文由 发表于 2023年3月3日 23:45:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75629166.html
匿名

发表评论

匿名网友

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

确定