Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/21.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
如何使用selenium python单击页面链接_Python_Selenium_Xpath_Css Selectors_Webdriverwait - Fatal编程技术网

如何使用selenium python单击页面链接

如何使用selenium python单击页面链接,python,selenium,xpath,css-selectors,webdriverwait,Python,Selenium,Xpath,Css Selectors,Webdriverwait,我的错误: selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"entrar"} 我有下面的html: <a role="menuitem" href="/login" class="_yce4s5"> <div class="_hgs47m">

我的错误:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"entrar"}
我有下面的html:

<a role="menuitem" href="/login" class="_yce4s5">
  <div class="_hgs47m">
     <div class="_10ejfg4u">
       <div>
         Entrar
       </div>
     </div>
 </div> 
</a>

但是它提出了一个非接触式的例外。

您是否尝试对第一个字母使用大写字母?类似于
'Entrar'
而不是
'Entrar'
您可以尝试
驱动程序。通过部分链接文本(“Entrar”)查找元素,因为链接中可能有其他文本。我喜欢通过xpath获取元素。这很容易。如果您有Firefox,只需使用附加的xpath查找器来查找网页上任何元素的xpath

要单击文本为entra的页面链接,因为元素是动态元素,您必须引导WebDriverWait使所需元素可单击,并且您可以使用以下任一解决方案:

  • 使用
    CSS\u选择器

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[role='menuitem'][href='/login'] > div > div > div"))).click()
    
  • 使用
    XPATH

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@role='menuitem' and @href='/login']//div[normalize-space()='Entrar']"))).click()
    
  • 注意:您必须添加以下导入:

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

您可以共享url吗?请提供完整的错误回溯,因为您的文本位于不包含链接的标记中。你可能想搜索“照布朗巴特曼说的做”。或者您可以按当前操作查找,然后通过两次转到父节点来获取标记。
驱动程序。通过链接文本(“entra”)查找元素(“entra”)
\Capital
E
谢谢,但这不是解决方案。@davilus,尝试
驱动程序。通过部分链接文本(“entra”)查找元素(“entra”)。锚定标记包含文本以外的其他标记。
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC