0

I write this code for web scraping work:

browser.find_element_by_class_name('open_all_j').click()

This code line gives me an error:

selenium.common.exceptions.ElementNotVisibleException: Message: element not visible

My full code:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common import keys, action_chains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from bs4 import BeautifulSoup as soup
import xlwt
def click_time():
    browser = webdriver.Chrome("./Drivers/chromedriver.exe")
    browser.implicitly_wait(20)
    browser.get("https://www.geegeez.co.uk/race-cards/#display=cards&day=0")

    timeTable = browser.find_elements_by_class_name('meeting')
    timeRow = timeTable[0].find_element_by_class_name("races")
    timeRowTable = timeRow.find_elements_by_class_name("race_card_race")
    timeRowTD = timeRowTable[0].find_elements_by_xpath("//table")
    x = timeRowTable[0].find_element_by_class_name("cardstable")
    y = x.find_element_by_class_name("racetime")
    y.click()
    print('\n', len(browser.find_elements_by_id('tabs-cards')), '\n')
    wait = WebDriverWait(browser, 20)
    elem = wait.until(EC.presence_of_element_located((By.ID, "tabs-cards")))
    #browser.find_element_by_xpath("//div[@class = 'open_all_r']").click() #find_element_by_class_name('open_all_r')
    browser.find_elements_by_class_name('open_all_j')[0].click()
    browser.find_elements_by_class_name('open_all_t')[0].click()

I tried to use:

browser.find_element_by_xpath("//div[@class = 'open_all_r']").click()

This code line gives me the same error.

Please help me...

Oleksandr Makarenko
  • 779
  • 1
  • 6
  • 18

3 Answers3

0

It can happen because of many reasons like element is not scrolled into view

driver.execute_script("arguments[0].scrollIntoView();", element)

If that does not help then try java script click

driver.execute_script("arguments[0].click();", element)
saurabh baid
  • 1,819
  • 1
  • 14
  • 26
  • There is no need of scrolling that element into view, it's clearly in view, you can check it because he has given the url as well. And also why you need to click via JS once once after you pulled the element into view ? – Rajagopalan Jul 30 '18 at 07:33
  • @Rajagopalan Java script click is only as an alternative if scrolling does not solve the issue. – saurabh baid Jul 31 '18 at 08:59
  • I am saying you don't have to scroll because it's clearly in view, open that site you can see. – Rajagopalan Jul 31 '18 at 09:00
0

first, try to catch what type of exception is this. Most probably this is due to 2 reasons- "reason-1" : may be page is not scrolled correctly. "solution" : use "javascriptexecutor" to scroll page(solution is provided by others above. "reason-2" : may be page is not fully loaded (I also got stuck in this problem). In this case, the solution is you have to wait for the page to load fully by using sleep method in the thread. thread.sleep(5000)

ASK
  • 1,136
  • 1
  • 10
  • 14
  • 1
    He has given the url as well . You can load and see what kind of problem is that rather than speculating it. I loaded that site and I found the targeted element is clearly ready for any action to be triggered. – Rajagopalan Jul 30 '18 at 07:31
-1

Because of numerous reason, it's possible that when you are trying to click on an element, it may not be visible / clickable by that time.

How I have overcome these situation is to have an extension method for FindElement and FindElements like this (Code is in c#, you may write equivalent in python):

public static void FindElement(this IWebDriver driver, By by, int timeout)
{
   if(timeout >0)
    {
        return new WebDriverWait(driver, TimeSpan.FromSeconds(timeout)).Until(ExpectedConditions.ElementToBeClickable(by));
    }

 return driver.FindElement(by);
}

 public static IReadOnlyCollection<IWebElement> FindElements(this IWebDriver driver, By by, int timeout)
{
   if(timeout >0)
    {
        return new WebDriverWait(driver, TimeSpan.FromSeconds(timeout)).Until(ExpectedConditions.PresenceOfAllElementsLocatedBy(by));
    }

 return driver.FindElements(by);
}

and you can call this like:

driver.FindElement(By.Xpath("xpath"), timeout).Click();
Raj
  • 664
  • 7
  • 23