使用硒 python 导致 css 选择器错误查找元素锚点 class 元素
Finding element anchor class element with selenium python causing css selector error
与HTML:
<a class="paginate_button next" aria-controls="tabcc" data-dt-idx="7" tabindex="0" id="tabcc_next">Next</a>
我正试图通过 class 到 select 'Next' 内部 HTML 抓住这个。我正在尝试:
next_page = self.driver.find_element_by_class_name('paginate_button next')
和
next_page = WebDriverWait(self.driver, 20).until(
EC.presence_of_element_located((By.CLASS_NAME, "paginate_button next"))
)
但都报错:
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".paginate_button next"}
(Session info: chrome=91.0.4472.114)
用 ID 做同样的事情似乎可行:
next_page = self.driver.find_element_by_id('tabcc_next')
但是,我需要它为 class 名称工作,具体来说就是我正在做的事情。
如有任何帮助,我们将不胜感激!
您正在尝试根据元素的 PARTIAL class 属性定位元素,而使用 find_element_by_class_name
定位元素需要精确的 class 属性值。
使用 css_selectors 或 XPath 可以通过部分属性值选择元素。
所以你可以使用 css_selector 代替。
next_page = self.driver.find_element_by_css_selector('.paginate_button.next')
或
next_page = WebDriverWait(self.driver, 20).until(
EC.presence_of_element_located((By.CSS_SELECTOR, ".paginate_button.next"))
)
或 XPath
next_page = self.driver.find_element_by_xpath("//a[contains(@class,'paginate_button next')]")
或
next_page = WebDriverWait(self.driver, 20).until(
EC.presence_of_element_located((By.XPATH, "//a[contains(@class,'paginate_button next')]"))
)
CLASS_NAME 不支持 spaces 正如您在 class 名称 paginate_button next
中看到的那样space.
如果您想继续相同的操作,则需要使用 CSS_SELECTOR
,下面的小改动应该适合您:
next_page = WebDriverWait(self.driver, 20).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "a.paginate_button.next"))
)
与HTML:
<a class="paginate_button next" aria-controls="tabcc" data-dt-idx="7" tabindex="0" id="tabcc_next">Next</a>
我正试图通过 class 到 select 'Next' 内部 HTML 抓住这个。我正在尝试:
next_page = self.driver.find_element_by_class_name('paginate_button next')
和
next_page = WebDriverWait(self.driver, 20).until(
EC.presence_of_element_located((By.CLASS_NAME, "paginate_button next"))
)
但都报错:
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".paginate_button next"}
(Session info: chrome=91.0.4472.114)
用 ID 做同样的事情似乎可行:
next_page = self.driver.find_element_by_id('tabcc_next')
但是,我需要它为 class 名称工作,具体来说就是我正在做的事情。
如有任何帮助,我们将不胜感激!
您正在尝试根据元素的 PARTIAL class 属性定位元素,而使用 find_element_by_class_name
定位元素需要精确的 class 属性值。
使用 css_selectors 或 XPath 可以通过部分属性值选择元素。
所以你可以使用 css_selector 代替。
next_page = self.driver.find_element_by_css_selector('.paginate_button.next')
或
next_page = WebDriverWait(self.driver, 20).until(
EC.presence_of_element_located((By.CSS_SELECTOR, ".paginate_button.next"))
)
或 XPath
next_page = self.driver.find_element_by_xpath("//a[contains(@class,'paginate_button next')]")
或
next_page = WebDriverWait(self.driver, 20).until(
EC.presence_of_element_located((By.XPATH, "//a[contains(@class,'paginate_button next')]"))
)
CLASS_NAME 不支持 spaces 正如您在 class 名称 paginate_button next
中看到的那样space.
如果您想继续相同的操作,则需要使用 CSS_SELECTOR
,下面的小改动应该适合您:
next_page = WebDriverWait(self.driver, 20).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "a.paginate_button.next"))
)