【问题标题】:Having trouble finding correct element text无法找到正确的元素文本
【发布时间】:2020-01-13 16:28:02
【问题描述】:

我对此很陌生,我已经被困了大约 2 天。我会尽力解释我的问题。

这是我要查找的元素:

<div id="popupContent" style="display: block; width: 300px;">
    <div style="width:100%;text-align:center">
        <div>
            <i class="material-icons" style="font-size:80px;color:#e33b3b">clear</i>
        </div>
        <div style="font-size:20px;color:rgba(255,255,255,0.5)">Password Incorrect</div>
    </div>
</div>

我需要它来查找显示清楚或显示密码不正确的位置。当密码不正确时,材料图标会说清楚,当密码正确时,会说完成。 div 样式表示成功!正确时。

这是我目前的代码:

import time
import sys
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException, ElementNotInteractableException, \
    StaleElementReferenceException, WebDriverException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

user_agent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.50 Safari/537.36'

options = webdriver.ChromeOptions()
options.add_experimental_option("useAutomationExtension", False)
options.add_argument("--disable-extensions")
#options.add_argument('headless')
options.add_argument(f'user-agent={user_agent}')
options.add_argument('log-level=2')
options.add_argument("--mute-audio")
options.add_argument("window-size=800,600")
driver = webdriver.Chrome(chrome_options=options)


def get_result(user, passw):
    driver.get("website")
    time.sleep(5)
    python_button = driver.find_element_by_id('profileLogin')
    python_button.click()
    password = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.ID, "accName")))
    password.send_keys(passw)
    username = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.ID, "accPass")))
    print(str(word_counter) + " " + passw)
    username.send_keys(user)
    login = driver.find_element_by_class_name('accountButton')
    login.click()
    result = driver.find_element_by_css_selector('i.material-icons')
    time.sleep(2)
    print(result.text)

当我打印(result.text)时,它每次都说“存储”。当我尝试其他一些事情时,它说的是“center_circle”。我正在尝试使 print(result.text) 显示清晰或密码不正确。我想我没有正确找到元素,但我不确定了。

【问题讨论】:

    标签: python-3.x selenium selenium-webdriver xpath selenium-chromedriver


    【解决方案1】:

    点击账户按钮后。诱导WebDriverWait() 和visibility_of_element_located() 并跟随xpath。

    print(WebDriverWait(driver,15).until(EC.visibility_of_element_located((By.XPATH,"//div[@id='popupContent']//i[@class='material-icons']"))).text)
    print(WebDriverWait(driver,15).until(EC.visibility_of_element_located((By.XPATH,"//div[@id='popupContent']//i[@class='material-icons']/following::div[1]"))).text)
    

    【讨论】:

      猜你喜欢
      • 2021-09-24
      • 1970-01-01
      • 2018-05-28
      • 1970-01-01
      • 2015-09-19
      • 2018-12-14
      • 1970-01-01
      • 1970-01-01
      • 2019-11-01
      相关资源
      最近更新 更多