[英]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.