簡體   English   中英

Selenium 動態 ID 和類

[英]Selenium dynamic ID and class

嘗試打開並發布到 FB。 很確定我要添加動態 ID 或類wearg。 Currtley 它打開/登錄到 facebook,然后我在我的 FB 主頁上看到一個黑色的覆蓋層。

from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
from selenium.webdriver.support.ui import WebDriverWait

import time

driver = webdriver.Chrome("C:\webdrivers\chromedriver.exe")
driver.get('http://facebook.com')

emailelement = driver.find_element(By.XPATH, './/*[@id="email"]')
emailelement.send_keys('email')

passelement = driver.find_element(By.XPATH, './/*[@id="pass"]')
passelement.send_keys('password')

elem = driver.find_element(By.XPATH,'.//*[@id="loginbutton"]')
elem.click()
print("Page open")

time.sleep(3)

statuselement= driver.find_elements(By.XPATH, ".//*[@id='_5qtp']")這就是事情開始向南的地方

time.sleep(3)

statuselement.send_keys("Post hi")
time.sleep(5)

buttons = driver.find_element_by_tag_name("button")
time.sleep(5)
for button in buttons:
    if button.text == "Post":
        button.click()

Facebook 上用於創建新帖子的 xpath id 和元素

<span class="_5qtp">Create Post</span>

單擊創建新帖子並打開文本字段后在此處輸入圖片說明

也許你可以使用:1-使用“開始”功能。

div[starts-with(@id,'something')] 

2-使用“包含”功能。

 div[contains(@id,'something')]

或者

3- 使用完整的 xpath,該 id 不是必需的:比如

/html/body/div[3]/div[2]/div[2]/div[2]/div[2]/div[2]/div[2]/div/div[4]/div[1]/div/div/div/div[1]/div/div/div/div[1]/div[2]/div[1]/form[1]/input[1]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM