无法点击该元素。

huangapple go评论100阅读模式
英文:

unable to click on the element

问题

这里我试图点击登录按钮,尝试使用xpath,尝试不同的xpath无法点击登录按钮,我正在使用Selenium Python在Chrome上。我还尝试使用WebDriverWait进行显式等待,但仍然不起作用。以下是我的Python代码:

import pandas as pd
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By

import time

driver = webdriver.Chrome(executable_path=r'C:\Drivers\chromedriver.exe')
driver.implicitly_wait(0.5)
driver.delete_all_cookies()

url="https://customer.nokia.com/s/"
driver.get(url)
time.sleep(20)
driver.find_element(by=By.XPATH, value="//button[@class='btn1 slds-button slds-button_outline-brand'][1]").click()
driver.find_element(by=By.XPATH, value="//a[@class='loginButton slds-button slds-button_brand']").click()

输出如下:

raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.JavascriptException: Message: javascript error: Cannot read 
properties of undefined 
(reading 'defaultView')
(Session info: chrome=110.0.5481.178)
Stacktrace:
...
if anyone can solve this error would very helpful to me.
thank you.

如果有人能解决这个错误,对我来说将非常有帮助。谢谢。

英文:

here I am trying to click on the Login button I try using xpath, try different xpath unable to click on the login button I am using selenium python on the chrome
I also try the explicit wait using WebDriverWait but still not work
here is my python code

import pandas as pd
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By

import time

driver = webdriver.Chrome(executable_path=r'C:\Drivers\chromedriver.exe')
driver.implicitly_wait(0.5)
driver.delete_all_cookies()

url="https://customer.nokia.com/s/"
driver.get(url)
time.sleep(20)
driver.find_element(by = By.XPATH, value = "//button[@class='btn1 slds-button slds- 
button_outline-brand'][1]").click()
driver.find_element(by=By.XPATH, value="//a[@class = 'loginButton slds-button slds- 
button_brand']").click()

无法点击该元素。

the output is

 raise exception_class(message, screen, stacktrace)
 selenium.common.exceptions.JavascriptException: Message: javascript error: Cannot read 
 properties of undefined 
 (reading 'defaultView')
 (Session info: chrome=110.0.5481.178)
  Stacktrace:
  Backtrace:
    (No symbol) [0x00AE37D3]
    (No symbol) [0x00A78B81]
    (No symbol) [0x0097B36D]
    (No symbol) [0x0097E0FB]
    (No symbol) [0x0097DFD0]
    (No symbol) [0x0097E250]
    (No symbol) [0x009B1F68]
    (No symbol) [0x009AFD0B]
    (No symbol) [0x009AE4D8]
    (No symbol) [0x009A3253]
    (No symbol) [0x009CB41C]
    (No symbol) [0x009A2B96]
    (No symbol) [0x009CB774]
    (No symbol) [0x009E1215]
    (No symbol) [0x009CB216]
    (No symbol) [0x009A0D97]
    (No symbol) [0x009A253D]
    GetHandleVerifier [0x00D5ABF2+2510930]
    GetHandleVerifier [0x00D88EC1+2700065]
    GetHandleVerifier [0x00D8C86C+2714828]
    GetHandleVerifier [0x00B93480+645344]
    (No symbol) [0x00A80FD2]
    (No symbol) [0x00A86C68]
    (No symbol) [0x00A86D4B]
    (No symbol) [0x00A90D6B]
    BaseThreadInitThunk [0x764300F9+25]
    RtlGetAppContainerNamedObjectPath [0x77447BBE+286]
    RtlGetAppContainerNamedObjectPath [0x77447B8E+238]

if anyone can solve this error would very helpfull to me.
thank you.

答案1

得分: 1

CSS选择器 a.loginButton 应该可以正常工作。我会添加一个 WebDriverWait 来确保页面在尝试单击任何内容之前已完全加载,但不要与隐式等待一起使用它。根据Selenium的负责人和开发者的说法,隐式等待是不良做法。我删除了隐式等待的调用,并添加了一个 WebDriverWait。只要需要等待某些内容,就使用它。请参阅文档

import pandas as pd
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By

driver = webdriver.Chrome(executable_path=r'C:\Drivers\chromedriver.exe')
driver.delete_all_cookies()

url = "https://customer.nokia.com/s/"
driver.get(url)
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.loginButton")).click()
英文:

The CSS selector, a.loginButton, should work just fine. I would add a WebDriverWait to be sure the page is fully loaded before trying to click anything BUT don't use it with implicit waits. Implicit waits are a bad practice according to the Selenium lead and developers. I removed the implicit wait call and added a WebDriverWait. Just use it whenever you need to wait for something. See the docs.

import pandas as pd
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By

driver = webdriver.Chrome(executable_path=r'C:\Drivers\chromedriver.exe')
driver.delete_all_cookies()

url = "https://customer.nokia.com/s/"
driver.get(url)
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.loginButton")).click()

答案2

得分: -1

`from selenium.webdriver.common.action_chains import ActionChains

ActionChains(driver).move_to_element(driver.find_element(By.CSS_SELECTOR,'a[class*="loginButton"]')).click().perform()`

英文:

`from selenium.webdriver.common.action_chains import ActionChains

ActionChains(driver).move_to_element(driver.find_element(By.CSS_SELECTOR,'a[class*="loginButton"]')).click().perform()`

答案3

得分: -1

以下是代码部分的中文翻译:

/*
   Bhairu 点击方法在正常点击无效时起作用
   它将执行操作点击,最后是 JavaScript 点击
*/
public static void BhairuClick(By 定位器, String 字段名)
{
    try {
        WebElement 正常点击 = DriverManager.getDriver().findElement(定位器);
        正常点击.click();
    } catch (Exception e) {
        // TODO: 处理异常
        try {
            logger.info("尝试使用 Action 类点击");
            Actions act = new Actions(getDriver());
            act.moveToElement(getDriver().findElement(定位器)).click().build().perform();
        } catch (Exception e2) {
            // TODO: 处理异常
            logger.info("尝试使用 Js 点击");
            JavascriptExecutor js = (JavascriptExecutor) getDriver();
            js.executeScript("arguments[0].click()", getDriver().findElement(定位器));
        }
    }
}

请注意,代码中的注释已被翻译成中文。如果您需要更多的帮助,请随时告诉我。

英文:
You can try with the below XPath 

//div[@class='header container']/child::div[2]/descendant::c-ncp-portal-header/descendant::a[5]

or use the below method.


/*
    The Bhairu Click method works when normal click is not performed 
    it will moved to the action click and final one is javascript click
    */
    public static void BhairuClick(By Locator,String FieldName)
    {
        try {
            WebElement NormalClick=DriverManager.getDriver().findElement(Locator);
            NormalClick.click();
        } catch (Exception e) {
            // TODO: handle exception
            try {
                logger.info("trying with the Action Class Click");
                Actions act=new Actions(getDriver());
                act.moveToElement(getDriver().findElement(Locator)).click().build().perform();
            } catch (Exception e2) {
                // TODO: handle exception
                logger.info("Trying with the Js Click");
                JavascriptExecutor js=(JavascriptExecutor)getDriver();
                js.executeScript("argument[0].click()", getDriver().findElement(Locator));
            }
        }
    }

huangapple
  • 本文由 发表于 2023年3月7日 10:41:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75657601.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定