Skip to content
Advertisement

How to use starts-with() in xpath to switch to frame using Selenium

Can anyone help me understand how i use the following java code in python and selenium:

new WebDriverWait(driver,10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[starts-with(@name, 'a-') and starts-with(@src, 'https://www.google.com/recaptcha')]"))); 
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.recaptcha-checkbox-checkmark"))).click();

I tried using string formatting and couldnt find a way of using starts-with() in python.

Advertisement

Answer

starts-with() is a function used for finding the web element whose attribute value gets changed on refresh or by other dynamic operations on the webpage. It should remain identical irrespective of the language binding e.g. Java, Python, C#, etc.


The equivalent of the following Java based lines of code:

new WebDriverWait(driver,10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[starts-with(@name, 'a-') and starts-with(@src, 'https://www.google.com/recaptcha')]"))); 
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.recaptcha-checkbox-checkmark"))).click();

in Python would be as follows:

WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(@name, 'a-') and starts-with(@src, 'https://www.google.com/recaptcha')]")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.recaptcha-checkbox-checkmark"))).click()
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement