【问题标题】:How to click few buttons with same xpath, class如何单击具有相同 xpath、类的几个按钮
【发布时间】:2021-11-10 23:32:43
【问题描述】:

我在点击领英页面上的每个按钮时遇到问题。在一些包含大量有关工作经验、学校、许可证信息的个人资料中,我们必须通过单击“显示更多按钮”来扩展这些信息。

Sample profile 1

Sample profile 2

我尝试了很多方法,例如通过 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 和字符s in word elements 来查找所有对象并将它们作为列表获取-然后您可以使用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


【解决方案1】:

我用自己的代码测试了页面,看来你可以得到所有按钮

items = driver.find_elements(By.XPATH, '//div[@class="profile-detail"]//button')

for item in items:
    driver.execute_script("arguments[0].click();", item)

但可能还有其他问题。页面使用"lazy loading",它可能需要使用向下滚动的 JavaScript 代码来加载所有组件。


这是我在 cmets 中的一些想法的完整代码。

我也尝试在部分中选择按钮,但并非所有方法都有效。
但也许它会对其他想法有用。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException, TimeoutException
import time

USERNAME = 'XXXXX'
PASSWORD = 'YYYYY'

url = 'https://www.linkedin.com/in/jakub-bialoskorski/?miniProfileUrn=urn%3Ali%3Afs_miniProfile%3AACoAABp5UJ8BDpi5ZwNGebljqDlYx7OXIKgxH80'

driver = webdriver.Firefox()

# -------------------------------------

driver.get(url)
time.sleep(5)

#wait = WebDriverWait(driver, 10)

cookies = driver.find_element(By.XPATH, '//button[@action-type="ACCEPT"]')
cookies.click()
time.sleep(1)

link = driver.find_element(By.XPATH, '//p[@class="authwall-join-form__subtitle"]/button')
link.click()
time.sleep(1)

login_form = driver.find_element(By.XPATH, '//div[@class="authwall-sign-in-form"]')
time.sleep(1)

username = login_form.find_element(By.XPATH, '//input[@id="session_key"]')
username.send_keys(USERNAME)
password = login_form.find_element(By.XPATH, '//input[@id="session_password"]')
password.send_keys(PASSWORD)
time.sleep(1)

#button = login_form.find_element(By.XPATH, '//button[@type="submit"]')
button = login_form.find_element(By.XPATH, '//button[contains(text(), "Zaloguj się")]')
button.click()
time.sleep(5)

# -------------------------------------

url = 'https://www.linkedin.com/in/jakub-bialoskorski/?miniProfileUrn=urn%3Ali%3Afs_miniProfile%3AACoAABp5UJ8BDpi5ZwNGebljqDlYx7OXIKgxH80'

#from selenium.webdriver.common.action_chains import ActionChains

driver.get(url)
time.sleep(5)

# -----------

print('... scrolling for lazy loading ...')
     
last_height = 0
while True:
    
    driver.execute_script("window.scrollTo(0, window.scrollY + window.innerHeight);")
    time.sleep(2)    

    new_height = driver.execute_script("return window.scrollY")
    if new_height == last_height:
        break
    last_height = new_height      

# -----------

def click_items(items):
    for item in items:
        print('text:', item.text)

        #print(item.get_attribute('innerHTML'))

        #print('... scrolling ...')
        #ActionChains(driver).move_to_element(item).perform()
       
        print('... scrolling ...')
        driver.execute_script("arguments[0].scrollIntoView(true);", item)
        
        #print('... clicking ...')
        #item.click()
        #time.sleep(1)

        print('... clicking ...')
        driver.execute_script("arguments[0].click();", item)
        time.sleep(1)
        
        print('----')
   
print('\n>>> Pokaż <<<\n')

#items = driver.find_elements(By.XPATH, '//button[contains(text(), "Pokaż")]')
#click_items(items)

print('\n>>> Doświadczenie - Pokaż więcej <<<\n')

#section = driver.find_elements(By.XPATH, '//section[@id="experience-section"]')
#items = driver.find_elements(By.XPATH, '//button[contains(text(), "zobacz wi")]')
items = driver.find_elements(By.XPATH, '//button[contains(@class, "inline-show-more-text__button")]')
click_items(items)

print('\n>>> Umiejętności i potwierdzenia - Pokaż więcej  <<<\n')

#section = driver.find_elements(By.XPATH, '//section[@id="experience-section"]')
items = driver.find_elements(By.XPATH, '//button[@data-control-name="skill_details"]')
click_items(items)

print('\n>>> Wyświetl <<<\n')

items = driver.find_elements(By.XPATH, '//button[contains(text(), "Wyświetl")]')
click_items(items)

print('\n>>> Rekomendacje <<<\n')

items = driver.find_elements(By.XPATH, '//button[@aria-controls="recommendation-list"]')
click_items(items)

print('\n>>> Osiągnięcia <<<\n')

print('--- projects ---')
items = driver.find_elements(By.XPATH, '//button[@aria-controls="projects-expandable-content"]')
click_items(items)

print('--- languages ---')
items = driver.find_elements(By.XPATH, '//button[@aria-controls="languages-expandable-content"]')
click_items(items)

# --- all buttons ---
#items = driver.find_elements(By.XPATH, '//div[@class="profile-detail"]//button')
#click_items(items)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-28
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多