【问题标题】:Python selenium didn't find css elementPython selenium 没有找到 css 元素
【发布时间】:2015-12-23 15:06:35
【问题描述】:

我在 python 中从 Selenium Ide 导入了代码。硒测试工作正常,无需点击项目,滚动无缝点击项目。 HTML硒代码:

    <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head profile="http://selenium-ide.openqa.org/profiles/test-case">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="selenium.base" href="http://www.amazon.com/" />
<title>New Test</title>
</head>
<body>
<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr><td rowspan="1" colspan="3">New Test</td></tr>
</thead><tbody>
<tr>
    <td>open</td>
    <td>/</td>
    <td></td>
</tr>
<tr>
    <td>clickAndWait</td>
    <td>css=[alt=&quot;Deals in Books&quot;]</td>
    <td></td>
</tr>

</tbody></table>
</body>
</html>

但在 Python 中,除非您滚动单击到所需的项目,否则它不起作用。

from selenium import webdriver
import unittest, time, re

class Untitled(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = "http://www.amazon.com/"
        self.verificationErrors = []
        self.accept_next_alert = True

    def test_untitled(self):
        driver = self.driver
        driver.get(self.base_url)
        driver.find_element_by_css_selector("a.feed-carousel-control.feed-right > span.gw-icon.feed-arrow").click()
        driver.find_element_by_css_selector("a.feed-carousel-control.feed-right > span.gw-icon.feed-arrow").click()
        driver.find_element_by_css_selector("a.feed-carousel-control.feed-right > span.gw-icon.feed-arrow").click()
        driver.find_element_by_css_selector("a.feed-carousel-control.feed-right > span.gw-icon.feed-arrow").click()
        driver.find_element_by_css_selector("[alt=\"Deals in Books\"]").click()

  def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)

我在不同的 selenium 定位器和 xpath css 中进行了测试,它可以工作,但在 python 中没有滚动点击元素不起作用。

【问题讨论】:

  • “不工作”是什么意思?没有什么?错误信息?你试过什么?究竟是什么问题?

标签: python html css selenium xpath


【解决方案1】:

首先,您可以wait for the element to become visible:

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

element = WebDriverWait(driver, 10).until(
    EC.visibility_of_element_located((By.CSS_SELECTOR, "[alt=\"Deals in Books\"]"))
)
element.click()

而且,如果您需要在单击之前滚动到该元素,请使用Action Chains

deals_in_books = driver.find_element_by_css_selector("[alt=\"Deals in Books\"]")

actions = ActionChains(driver)
actions.move_to_element(deals_in_books).click().perform()

如果需要,scroll into view of the element:

browser.execute_script("arguments[0].scrollIntoView();", deals_in_books)

【讨论】:

    猜你喜欢
    • 2021-06-22
    • 1970-01-01
    • 2021-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多