下面是使用Selenium和Firefox在Python中下载网页的代码。页面的一部分是用Javascript呈现的,所以我想等到一个短语呈现出来。
这是我正在使用的代码:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
def firefoxget(url: str, file: str):
browser = webdriver.Firefox()
browser.get(url)
WebDriverWait(browser,30).until(EC.text_to_be_present_in_element(By.XPATH,"//*[contains(text(),'In Stock')]"))
f = open(file, "w")
f.write(browser.page_source)
f.close()
browser.close()
问题是,在find_element行中,我得到了“WebDriverWait
()从1到3个位置参数,但给出了6”。我对python并不熟悉,不幸的是,这对我来说毫无意义,因为我无法理解6个论点是从何而来的。
发布于 2021-08-20 06:09:33
而不是
WebDriverWait(browser,30).until(EC.text_to_be_present_in_element(By.XPATH,"//*[contains(text(),'In Stock')]"))
试试这个:
WebDriverWait(browser,30).until(EC.visibility_of_element_located((By.XPATH,"//*[contains(text(),'In Stock')]")))
基本上,您缺少了一个打开的和结束的括号。见By.XPATH
附近
发布于 2021-08-19 21:12:11
用于EC.text_to_be_present_in_element(By.XPATH,"//*[contains(text(),'In Stock')]")
的语法是错误的。
语法是:
class selenium.webdriver.support.expected_conditions.text_to_be_present_in_element(locator, text)
检查指定元素中是否存在给定文本的期望。定位器,文本
但我没有看到您期望在该元素中看到的文本值。
也许你只是想等到这个元素变得可见?
如果是这样的话,您应该使用visibility_of_element_located
预期条件。
在你的情况下:
WebDriverWait(browser,30).until(EC.visibility_of_element_located(By.XPATH,"//*[contains(text(),'In Stock')]"))
https://stackoverflow.com/questions/68854303
复制相似问题