python - Selenium 使用Python : how to correctly click() an element?

标签 python selenium xpath webdriverwait xpath-1.0

在学习如何使用 selenium 时,我尝试单击一个元素,但没有任何反应,并且无法到达下一页。这是相关页面:http://buyme.co.il我正在尝试点击:הרשמה

我成功打印了所需的元素(הרשמה),所以我想我到达了页面中的正确位置。但“click()”不起作用。 第二个跨度<span>הרשמה</span>是我想要点击的:

<li data-ember-action="636">
        <a>
            <span class="seperator-link">כניסה</span>
            <span>הרשמה</span>
        </a>
 </li>
for elem in driver.find_elements_by_xpath('//* [@id="ember591"]/div/ul[1]/li[3]/a/span[2]'):
    print (elem.text)
    elem.click()

也尝试过这个:

driver.find_element_by_xpath('//*[@id="ember591"]/div/ul[1]/li[3]/a').click()

我希望到达包含注册字段的“灯箱”。 关于实现这一目标的最佳方法有什么想法吗?

最佳答案

Explicit Waits - 显式等待是您定义的代码,用于等待特定条件发生,然后再继续处理代码。

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

browser = webdriver.Chrome()
browser.get("https://buyme.co.il/")

WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.ID, 'ember591')))

elm = browser.find_elements_by_xpath('//div[@id="ember591"]/div/ul[1]/li[3]/a')

elm[0].click()

enter image description here

更新:

WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, 'login')))

email = browser.find_elements_by_xpath("//form[@id='ember1005']/div[1]/label/input")
email[0].send_keys("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7213101132151f131b1e5c111d1f" rel="noreferrer noopener nofollow">[email protected]</a>")

password = browser.find_elements_by_xpath("//form[@id='ember1005']/div[2]/label/input")
password[0].send_keys("test1234567")

login = browser.find_elements_by_xpath('//form[@id="ember1005"]/button')
login[0].click()

enter image description here

关于python - Selenium 使用Python : how to correctly click() an element?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56507431/

相关文章:

python - 使用角坐标收缩多边形

python - 使用 gtk 对话框选择器小部件列出远程目标的文件

c# - 为什么这个值不是有效的 DateTime 对象?

python - Selenium 蟒 : find_element_by_css_selector() using :contains()

xml - XPath:起始于和值列表

python - 类型错误 : an integer is required python

python - Python 3 中的模式匹配字典

python - 尝试根据文本查找链接时的 Selenium 错误

python - WSGIServerException : [Errno 8] nodename nor servname provided, 或未知