python - 如何连续向下滚动页面直到找到某个元素? Python Selenium

标签 python selenium

我对 Linkedin 技能部分的加载更多按钮感到困惑。我在查找按钮的 xpath 时收到此错误: selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to located element

问题是我的元素在页面上不可见,因此我一直在尝试找到一种在页面上连续滚动直到按钮可见的方法。我正在尝试对多个配置文件进行 forloop。

我的相关代码:

import parameters
from selenium.webdriver.common.action_chains import ActionChains
from selenium import webdriver

ChromeOptions = webdriver.ChromeOptions()
driver = webdriver.Chrome('C:\\Users\\Root\\Downloads\\chromedriver.exe')

driver.get('https://www.linkedin.com/login?fromSignIn=true&trk=guest_homepage-basic_nav-header-signin')
sleep(0.5)

username = driver.find_element_by_name('session_key')

username.send_keys(parameters.linkedin_username)
sleep(0.5)

password = driver.find_element_by_name('session_password')
password.send_keys(parameters.linkedin_password)
sleep(0.5)

sign_in_button = driver.find_element_by_xpath('//button[@class="btn__primary--large from__button--floating"]')
sign_in_button.click()

driver.get('https://www.linkedin.com/in/kate-yun-yi-wang-054977127/?originalSubdomain=hk')

loadmore_skills=driver.find_element_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"]')

尝试 1.

actions = ActionChains(driver)
actions.move_to_element(loadmore_skills).perform()
#actions.move_to_element_with_offset(loadmore_skills, 0, 0).perform()
loadmore_skills.click()

使用 actions.move_to_element 页面会滚动到元素正下方,因此该元素不再可见,并且随后会发生相同的错误。

我也尝试过 move_to_element_with_offset,但这并没有改变页面滚动到的位置。

2.

coordinates = loadmore_skills.location_once_scrolled_into_view 
driver.execute_script('window.scrollTo({}, {});'.format(coordinates['x'], coordinates['y']))

这会返回相同的错误消息

3.

loadmore_skills=WebDriverWait(driver,30).until(EC.element_to_be_clickable((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"]')))

这也会返回相同的错误。

4.

driver.execute_script("arguments[0].scrollIntoView();", loadmore_skills)

不知道还能怎么做。非常感谢您的帮助。

更新: 尝试@Dipak解决方案将我移动到页面底部,并且无法单击该元素: enter image description here

错误回溯:

Traceback (most recent call last):
  File "C:/Users/Root/PycharmProjects/Quant/skillstest.py", line 60, in <module>
    EC.element_to_be_clickable((By.XPATH, "//span[text()='Show more']")))
  File "C:\Users\Root\PycharmProjects\Quant\venv\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 

最佳答案

这是按照您在评论中所述解决您的问题的完整工作代码。

其他人建议的答案实际上是向下滚动到页面底部,然后也给出错误。然后我注意到,如果您滚动到底部,则只有底部部分加载,而不是中间的所有部分加载。 (迪帕克的回答对我来说也不起作用。也许这对你和我来说都是一个解决问题:) 正如他在聊天中所说的那样)

因为您想要的内容存在于页面之间而不是底部。所以总是只有底部部分加载。所以现在我们需要做点别的事情。

我们现在想要的是向下滚动到您想要的部分。为了进行自定义向下滚动,我使用了 driver.execute_script("scroll(0, 1600)") 。我还从代码中删除了所有不必要的东西,并使其非常简单和直接。

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
import time

driver = webdriver.Chrome(executable_path=r"C:\Users\intel\Downloads\Setups\chromedriver")

driver.get('https://www.linkedin.com/login?fromSignIn=true&trk=guest_homepage-basic_nav-header-signin')
driver.maximize_window()

WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.NAME, "session_key"))).send_keys("EMAIL")
WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.NAME, "session_password"))).send_keys("PASSWORD")
WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.XPATH, "//button[@class='btn__primary--large from__button--floating']"))).click()

driver.get("https://www.linkedin.com/in/kate-yun-yi-wang-054977127/?originalSubdomain=hk")
driver.maximize_window()

driver.execute_script("scroll(0, 1600)")
time.sleep(5)
buttonClick = driver.find_element_by_xpath("/html/body/div[6]/div[4]/div[3]/div/div/div/div/div[2]/main/div[2]/div[6]/div/section/div[2]/button/span[1]").click()

还可以使用--headless浏览器更快地加载任务。如果可能的话,请使用 css_selectors 而不是 XPATH's,因为它们是用于抓取目的的最慢的定位器。

关于python - 如何连续向下滚动页面直到找到某个元素? Python Selenium ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61349971/

相关文章:

python / Selenium : Switch to an alert and verify the text within

symfony - 需要在测试代码中使用codeception参数

python - 为什么这个 While 循环会终止?

c# - Selenium 通过 XPath 获取元素

python - 如何在Python中从Linux终端显示图形?

python:正确使用字典

javascript - Headless Chrome 和 document.location.href 更改

python - 如何创建可以解码 SSL 流量的代理?

python - 如何检查是否按下了键盘修饰符(Shift、Ctrl 或 Alt)?

python - python中的字符串匹配