【发布时间】:2021-11-10 23:32:43
【问题描述】:
我在点击领英页面上的每个按钮时遇到问题。在一些包含大量有关工作经验、学校、许可证信息的个人资料中,我们必须通过单击“显示更多按钮”来扩展这些信息。
我尝试了很多方法,例如通过 Xpath 搜索元素,然后循环它们以单击页面上的每个按钮,但它不起作用 - 因为每个按钮类都与我们可以使用 selenium 找到的其他元素相同。我认为第一个“显示更多”按钮始终用于 experiane 部分,并且该代码可以点击它:
self.driver.execute_script("arguments[0].click();", WebDriverWait(
self.driver, 3).until(EC.element_to_be_clickable((By.XPATH, "//li-icon[@class='pv-profile"
"-section__toggle-detail-icon']"))))
然后我们有教育部分、许可证和认证部分 - 这让我很麻烦。临时解决方案是点击包含字符串的元素:
self.driver.find_element_by_xpath("//*[contains(text(), 'Pokaż więcej')]").click()
或
self.driver.find_element_by_xpath("//*[contains(text(), 'Pokaż 1 uczelnię więcej')]").click()
迟早我知道代码有很多限制。有没有人知道如何解决这个问题?
解决方案
containers = self.driver.find_elements_by_xpath("//li-icon[@class='pv-profile-section__toggle-detail-icon']")
for button in containers:
self.driver.execute_script('arguments[0].click()', button)
【问题讨论】:
-
也许使用
find_elements_by_xpath和字符sin wordelements来查找所有对象并将它们作为列表获取-然后您可以使用for-loop 来一一单击。 -
您的链接需要登录才能看到页面(PL: twoje linki wymagają zalogowania więc może ich nie zobaczyć)
-
是的,我在此操作期间已登录,所以这不是问题。
-
您是否尝试在单词
elements中使用find_elements_by_xpath和字符s?find_element_by_xpath没有s在单词element中只能给出页面上的第一个元素(如果您使用driver运行它),或者您必须首先获得所有containers的报价,然后再使用for-loop(for single_container in all_containers) 和single_container.find_element_by_xpath获取每个single_container中的第一个元素 -
是的,我试过了。
buttons = WebDriverWait(self.driver, 10).until( EC.presence_of_all_elements_located((By.XPATH, "//button[@class='pv-profile-section__card-action-bar pv-skills-section__additional-skills artdeco-container-card-action-bar artdeco-button artdeco-button--tertiary artdeco-button--3 artdeco-button--fluid artdeco-button--muted']"))) for but in buttons: button = WebDriverWait(container, 20).until(EC.element_to_be_clickable((By.XPATH, "//li-icon[@class='pv-profile-section__toggle-detail-icon']"))) button.click()没用
标签: python selenium web-scraping