如何使用 xpath 中的 starts-with() 切换到使用 Selenium 的框架

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

任何人都可以帮助我了解我如何在 python 和 selenium 中使用以下 java 代码:

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();

我尝试使用字符串格式,但找不到在 python 中使用 starts-with() 的方法。

starts-with() 是一个 函数,用于查找其属性值在刷新或网页上的其他动态操作时发生变化的网络元素。无论语言绑定如何,它都应该保持相同,例如JavaPythonC#


相当于以下基于 Java 的代码行:

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();

Python 中将如下所示:

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()