使用 python 和 selenium 如何获取值不是 utf-8 的占位符的值?

Using python and selenium how can i get the value of placeholder where the value is not utf-8?

我试图获取电子邮件地址并单击以下屏幕截图中的刷新按钮。但我收到错误。

我的代码如下:

from selenium import webdriver
url = 'http://od.obagg.com/ '

driver = webdriver.Chrome(executable_path='chromedriver')
driver.get(url)

s = driver.find_element_by_id('//*[@id="shortid"]').get_attribute('placeholder')
print(s)

根据检查结果,我尝试并尝试了很多方法来获取该电子邮件字段值并单击刷新按钮。但是还是不行。

有人知道什么技巧可以分享吗?

可能是元素被禁用的原因,另外,find_element_by_id('//*[@id="shortid"]')不正确。它可以是:

  • find_element_by_xpath('//*[@id="shortid"]')
  • find_element_by_id("shortid") ?

以下对我有用:

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait

driver = webdriver.Chrome(executable_path='chromedriver')
driver.maximize_window()
driver.get('http://od.obagg.com/')
wait = WebDriverWait(driver, 10)
el = wait.until(ec.visibility_of_element_located((By.ID, "shortid")))
placeholder = el.get_attribute("placeholder")
email = el.get_attribute('value')
print(placeholder, email)
# 请等待分配临时邮箱 -_ylp06tc@xxx.xxx

如果您需要 10 个不同的电子邮件,您可以使用:

from time import sleep
for x in range(10):
    driver.find_element_by_id("refreshShortid").click()
    sleep(0.15) # you may have to increase this value to give enough time to generate the new email
    new_email = driver.find_element_by_id("shortid").get_attribute('value')
    print(new_email)