0

I'm using Python and Selenium to automate a test case. The problem is that when I reach test_press_add_to_cart the Webdriver doesn't see the element with the xpath:

//*[@id="fybAddCartEvent"]

What could be the problem ?

The exception is :

"TimeoutException: Message"

The HTML of the element is:

<a href="#" class="button" data-product_id="18542" data-wp_nonce="7c3d595f98" id="fybAddCartEvent">
Add to cart</a>

And the script:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.webdriver import WebDriver
from selenium.webdriver.common.action_chains import ActionChains
from HTMLTestRunner import HTMLTestRunner

class Fotball_add_to_cart(unittest.TestCase):
    @classmethod
def setUpClass(inst):
    inst.driver = webdriver.Chrome('C:/chromedriver/chromedriver.exe')
    driver = inst.driver
    driver.get("http://ak:[email protected]/football/")
    inst.driver.maximize_window()
    time.sleep(5)

    #click on "View All Fotball Products"
def test_click_on_view_all_fotball_products(self):
    viewProductsXpath = "a.woocommerce-nested-category-layout-see-more"
    self.viewProductsElement = self.driver.find_element_by_css_selector(viewProductsXpath)
    self.viewProductsElement.click()                                                      
    time.sleep(7)
    #select a product

def test_select_a_product_and_view_details(self):
    #select product
    tshirtXpath = "//a[@href=\"http://uat.athleticknit.com/product/f810/F810-000/\"]"
    self.tshirtElement = self.driver.find_element_by_xpath(tshirtXpath)
    self.tshirtElement.click()
    time.sleep(60)

def test_press_add_to_cart(self):
    #press add to cart
    driver = self.driver
    addToCartXpath = '//*[@id="fybAddCartEvent"]'
    self.addToCartElement =WebDriverWait(driver, 20).until(lambda driver: driver.find_element_by_xpath(addToCartXpath))
    self.addToCartElement.click()
    time.sleep(5)
@classmethod    
def tearDownClass(inst):
    inst.driver.stop_client()
    inst.driver.close()
    inst.driver.quit()
2
  • Have you checked to see if it's inside an iframe? Commented Mar 29, 2017 at 17:47
  • @alisu245 , are you facing the same issue ? Commented Mar 31, 2017 at 16:34

1 Answer 1

1

Actually you have used Explicit wait right here

self.addToCartElement =WebDriverWait(driver, 20).until(lambda driver: driver.find_element_by_xpath(addToCartXpath))

So it is waiting for your condition until 20 second and if condition not satisfying then showing TimeoutException.

If your requirement to wait for 'Add To cart' then try the following way -

element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.xpath, addToCartXpath)))

Note :- If you have element id then please locate element by ID instead of xpath

 element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "fybAddCartEvent")))

 element.click()
Sign up to request clarification or add additional context in comments.

1 Comment

I tried bui it is not working, i tried to identify after id too. My requirment is to wait for Add To Cart to appear, then click on it. I put a sleep of 60 seconds in the code prior to searching for the element so there is plenty of time and the element indeed appears but cannot be identified by path.or id

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.