【问题标题】:"Unable to find element error" even if this exists“无法找到元素错误”即使存在
【发布时间】:2022-06-15 09:35:07
【问题描述】:

有没有办法在“Like 4 Like”网站上点击这个“like”按钮?因为我不能通过 xpath 或完整的 xpath 来做到这一点,甚至不能使用“webdriverwait”。我总是收到错误Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id='likebutton52335" class="div-center text-center']"} (Session info: chrome=100.0.4896.127),即使我知道它存在。

                TEST = 0
                while TEST < 1:
                likebutton = driver1.page_source
                time.sleep(10)
                #BUTTON = re.findall('id="/html/body/div[9]/div/div/div/div[1]/div/div/div[4]/div/div[2]/div[2]/div[4]/div/div/div[1]/div[1]/span[5]"', likebutton, re.MULTILINE)
                BUTTON = re.findall('id="likebutton(.+?)"><a', likebutton, re.MULTILINE)
                if BUTTON == []:
                    TEST += 1
                for num in BUTTON:
                    try:
                        button = 'likebutton%s' % num
                        ref = "//*[@id='%s']" % button
                        time.sleep(3)
                        #WebDriverWait(driver1, 10).until(EC.element_to_be_clickable((By.XPATH, ref))).click()
                        #wait.until(EC.element_to_be_clickable((By.XPATH, "//span[contains(text(),'Like')]"))).click()
                        driver1.find_element(By.XPATH, ref).click()
                        #driver1.find_element(By.XPATH, "/html/body/div[9]/div/div/div/div[1]/div/div/div[4]/div/div[2]/div[2]/div[4]/div/div/div[1]/div[1]/span[5]").click()
                        time.sleep(2)
                        window_after = driver1.window_handles[1]
                        driver1.switch_to.window(window_after)
                        time.sleep(1)
                        likebutton = driver1.page_source
                        time.sleep(3)

like4like button screen

【问题讨论】:

    标签: python python-3.x xpath


    【解决方案1】:

    您的问题在您的正则表达式中!

    实际上你的正则表达式配置为:

    id="(number"...)"<a
    
    That explain this value for the regex in the error: 
    52335" class="div-center text-center']"
    

    所以你可以从 HTML 标签中得到其他参数。

    您需要通过以下方式更改您的正则表达式:

    id="likebutton(.+?)"
    

    就像你得到双引号之间的内容一样。

    结果:

          TEST = 0
          while TEST < 1:
                    likebutton = driver1.page_source
                    time.sleep(3)
    // This Line was modified
                    BUTTON = re.findall('id="likebutton(.+?)"', likebutton, re.MULTILINE)
    ////////////////////////
                    if BUTTON == []:
                        TEST += 1
                    for num in BUTTON:
                        try:
                            button = 'likebutton%s' % num
                            ref = "//*[@id='%s']" % button
                            driver1.find_element(By.XPATH, ref).click()
                            time.sleep(2)
                            window_after = driver1.window_handles[1]
                            driver1.switch_to.window(window_after)
                            time.sleep(1)
                            likebutton = driver1.page_source
                            time.sleep(3)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-18
      • 1970-01-01
      相关资源
      最近更新 更多