Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在Selenium中定位标记按钮(角度组件)?_Python_Selenium_Xpath_Css Selectors_Webdriverwait - Fatal编程技术网

Python 如何在Selenium中定位标记按钮(角度组件)?

Python 如何在Selenium中定位标记按钮(角度组件)?,python,selenium,xpath,css-selectors,webdriverwait,Python,Selenium,Xpath,Css Selectors,Webdriverwait,这是我试图定位的按钮的HTML,它在我看来是一个角度组件: <tab-button class="tab-button _ngcontent-jne-2 _nghost-jne-3" focusitem="" role="tab" aria-selected="false" tabindex="-1" aria-disabled="false" aria-label=&q

这是我试图定位的按钮的HTML,它在我看来是一个角度组件:

<tab-button class="tab-button _ngcontent-jne-2 _nghost-jne-3" focusitem="" role="tab" aria-selected="false" tabindex="-1" aria-disabled="false" aria-label="Offline Reporting">
    <!---->
    <div class="content displayed-value _ngcontent-jne-3">Offline Reporting</div>
    <!---->
    <material-ripple class="_ngcontent-jne-3">
        <div class="__acx-ripple" style="top: -103px; left: -45px; transform: translate(17px, -1px) scale(0.88156);"></div>
        <div class="__acx-ripple" style="top: -101px; left: -62px; transform: translate(34px, -3px) scale(0.88156);"></div>
    </material-ripple>
</tab-button>
以上这些都不起作用。你能指出正确的方法吗?我必须找到选项卡按钮(在众多按钮中),该按钮具有
aria标签
作为离线报告

快速编辑

请注意,我使用SeleniumWeb驱动程序也是出于同样的目的,并且我尝试加载的页面是一个有角度的页面。根据我的理解,内容在这里是动态呈现的,因此即使我可以在元素检查器中看到它,它也不能通过
find\u element
函数来定位

以下是相同的快照:

请注意,我已经尝试了当前答案中提到的所有内容。。。等待页面完全加载,即使页面完全加载,我也尝试了50秒的延迟,如下所示:

WebDriverWait(driver, 50).until(EC.visibility_of_element_located((By.XPATH, "//tab-button[contains(@class, 'tab-button') and @aria-label='Offline Reporting']")))

但这也导致了超时。我试图通过这些行指出的一点是,这是一个动态呈现的页面,因此要处理它,还需要一些其他中间件机制,如

尝试等待按钮变为可见和可单击:

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

button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//tab-button[@aria-label='Offline Reporting']")))
button.click()

要定位文本为脱机报告的元素,可以使用以下任一选项:

  • 使用
    css\u选择器

    element = driver.find_element_by_css_selector("tab-button.tab-button[aria-label='Offline Reporting']")
    
  • 使用
    xpath

    element = driver.find_element_by_xpath("//tab-button[contains(@class, 'tab-button') and @aria-label='Offline Reporting']")
    

由于元素是一个元素,理想情况下,要定位需要归纳的元素,使
元素可点击()
,您可以使用以下任一选项:

  • 使用
    CSS\u选择器

    element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "tab-button.tab-button[aria-label='Offline Reporting']")))
    
  • 使用
    XPATH

    element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//tab-button[contains(@class, 'tab-button') and @aria-label='Offline Reporting']")))
    
  • 注意:您必须添加以下导入:

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

按你说的那样试过。。。运气不好,它仍然找不到这个元素。@ShivamSahil它是否位于iframe中?实际上,它是一个有角度的站点,我正在浏览许多文档,这时我意识到废弃动态站点有点棘手。对于您的问题,它不是位于iframe内部,而是位于@ShivamSahil内部,“/*[name()='tab-button'和@aria label='Offline Reporting']”如何??这不起作用。。。实际上,这些页面首先请求外部API,然后加载。。。所以基本上由于某些原因,它无法定位它,因为即使在加载时,它们也在动态加载…嗨,非常感谢您的回答。。。虽然你的回答很有帮助,但并没有解决我的问题。。。使用您提到的内容也会超时,因为页面是通过API请求动态呈现的,并且并非所有内容都是实际内容,即使我可以在inspector中检查它们,但它们不是通过selenium找到的。@ShivamSahil签出更新的答案并让我知道状态HI。。。我也试过了。。。这也不管用(另外,我还添加了一些有问题的编辑,只需查看它以获得更多澄清回溯(最近一次调用最后一次):文件“”,第1行,在element=WebDriverWait(driver,20)中。直到(EC.element可点击((By.XPATH,//tab button[包含(@class,'tab button')和@aria label='Offline Reporting'])文件)“C:\Users\DELL\AppData\Local\Programs\Python36\lib\site packages\selenium\webdriver\support\wait.py”,第80行,直到引发超时异常(消息、屏幕、堆栈跟踪)selenium.common.exceptions.TimeoutException:消息:您看到了什么错误?TimeoutException无法在代码试用中看到TimeoutException。如果是这样,则存在严重问题。
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC