Raf*_*ini 7 python selenium xpath css-selectors webdriverwait
我正在尝试使用 Selenium 查找元素,但没有找到。请遵循 HTML 代码:
\n\n<div aria-disabled="false"\n data-tb-test-id="DownloadCrosstab-Button"\n role="button"\n tabindex="0"\n style="font-size: 12px; font-weight: normal; color: rgba(0, 0, 0, 0.7); display: inline-block; padding: 0px 24px; position: relative; text-align: center; border-style: solid; border-width: 1px; border-radius: 1px; height: 24px; line-height: 22px; min-width: 90px; box-sizing: border-box; outline: none; white-space: nowrap; user-select: none; cursor: default; background-color: rgba(0, 0, 0, 0); border-color: rgb(203, 203, 203); margin-top: 8px; width: 100%; -webkit-tap-highlight-color: transparent;"\n>Tabela de refer\xc3\xaancia cruzada</div>\nRun Code Online (Sandbox Code Playgroud)\n\n我已经尝试过以下代码:
\n\nx = browser.find_element_by_id("Downloadcrosstab")\nx = browser.find_element_by_link_text("Downloadcrosstab-Button")\nx = browser.find_element_by_class_name(\'Crosstab\')\nRun Code Online (Sandbox Code Playgroud)\n\n但我得到了同样的错误:
\n\nNoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":".Crosstab"}\n (Session info: chrome=75.0.3770.142)\nRun Code Online (Sandbox Code Playgroud)\n
Deb*_*anB 13
该元素似乎是动态元素,要识别需要引发WebDriverWait以使所需元素可单击的元素,您可以使用以下解决方案之一:
\n\n使用CSS_SELECTOR:
x = WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div[data-tb-test-id=\'DownloadCrosstab-Button\'][role=\'button\']"))).click()\nRun Code Online (Sandbox Code Playgroud)使用XPATH:
x = WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@data-tb-test-id=\'DownloadCrosstab-Button\' and text()=\'Tabela de refer\xc3\xaancia cruzada\']")))\nRun Code Online (Sandbox Code Playgroud)注意:您必须添加以下导入:
\n\nfrom selenium.webdriver.support.ui import WebDriverWait\nfrom selenium.webdriver.common.by import By\nfrom selenium.webdriver.support import expected_conditions as EC\nRun Code Online (Sandbox Code Playgroud)\n\n注意:使用 Chrome 时,您可以在 Selenium \xe2\x80\x9cselenium.common.exceptions.NoSuchElementException\xe2\x80\x9d 中找到相关讨论
\n
我实际上喜欢为我的测试定位器添加自定义 html 属性。
然后您可以使用 xpath 来定位它们,例如//*[@data-tb-test-id="DownloadCrosstab-Button"]
| 归档时间: |
|
| 查看次数: |
36686 次 |
| 最近记录: |