简体   繁体   English

我无法使用Python / Selenium单击网页中的任何元素

[英]I can't click any element in a webpage using Python/Selenium

I'm webscrapping this webpage: https://www.skoob.com.br/o-pequeno-principe-693ed56597.html but for some reason I can't click anything an I don't know why. 我正在对该网页进行爬网: https ://www.skoob.com.br/o-pequeno-principe-693ed56597.html,但是由于某些原因我无法单击任何东西,我不知道为什么。

I've tried to use both implict and explicit wait, find element by xpath and text. 我试图同时使用隐式和显式等待,通过xpath和text查找元素。 It just doesn't work. 就是行不通。

for l in range(1, 107):
    wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="estante"]/div[3]/div['+str(l)+']/a/img')))
    driver.find_element_by_xpath('//*[@id="estante"]/div[3]/div['+str(l)+']/a/img').click()

    driver.find_element_by_link_text('Leitores').click()

I've tried by text and it's giving me timeoutexception. 我已经尝试过文字了,这给了我timeoutexception。 NoSuchElementException sometimes. 有时会出现NoSuchElementException。 Also clicking with pyautogui doesn't work, Python actually seems to ignore the pyautogui line. 另外用pyautogui单击也不起作用,Python实际上似乎忽略了pyautogui行。

The element you described in your post vs. in your comment don't appear to be the same thing. 您在帖子和评论中描述的元素似乎不是一回事。 Here's how I would implement a simple wait and click: 这是我实现简单的等待并单击的方法:


public void ElementExists(this IWebDriver driver, By by)
{
    try
    {
        // element exists, so return true
        return driver.FindElements(by).Count > 0;
    }
    catch (NoSuchElementException e)
    {
        // element does not exist so return false
        return false;
    }
}

// use ElementExists in this method
public void WaitForElementToExist(this IWebDriver driver, By by)
{
    // initialize wait to 30 seconds
    var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
    wait.Until(d => d.ElementExists(by);
}

// lastly -- wait and click
public void WaitForElementAndClick(this IWebDriver driver, By by)
{
    driver.WaitForElementToExist(by);
    driver.FindElement(by).Click();
}

With this code implementation, you can accomplish what you are trying to achieve like this: 通过此代码实现,您可以像这样完成您要实现的目标:

driver.WaitForElementAndClick(By.XPath("//a[text()='2.856']"));

This will wait for the element to exist & click it, based on the HTML sample you provided in your comment. 这将根据您在评论中提供的HTML示例,等待元素存在并单击它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用硒和python单击网页上的元素 - How can i click the element on webpage using selenium with python Python Selenium 在网页上找不到任何元素 - Python Selenium can't find any element on a webpage 与移动用户代理一起使用时,无法单击使用 python selenium 的网页元素 - Can't click on webpage element using python selenium when used with mobile user-agent Selenium无法点击网页python的按钮 - Selenium can't click the button of a webpage python 如何使用Selenium-Python突出显示网页上的元素? - How can I highlight element on a webpage using Selenium-Python? Python Selenium PhantomJS-禁用Javascript后,我无法单击任何内容或获取任何元素 - Python Selenium PhantomJS - After disabling Javascript I can't click on anything or get any element 无法使用 Python 的 Selenium 点击输入元素 HTML - Can't click on an input element HTML using Selenium for Python 在Python中使用Selenium无法通过任何定位器找到元素 - Can't find element by through any locator using Selenium in Python 在CSS上将CSS选择器与Selenium结合使用时找不到任何元素 - Can't find any element using CSS selector with Selenium on Python 无法使用 selenium/python 单击特定元素(尝试了 iframe 以及我知道或在这里找到的所有方法) - Can't click on a specific element using selenium/python (tried iframe and all the methods that I know or found here)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM