Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 74 additions & 36 deletions linkedin_mcp_server/scraping/extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1487,6 +1487,21 @@ async def _resolve_message_compose_box(self) -> Any | None:
candidate_count if candidate_count is not None else "unknown",
)

# patchright quirk: locator.wait_for(state="visible") times out on
# the contenteditable compose div even though count() > 0 and the
# element is fully visible by every CSS/DOM criterion (display:block,
# visibility:visible, opacity:1, non-zero bbox, no inert ancestor).
# This appears to be a patchright bug with React-hydrated contenteditable
# elements in isolated worlds. Skip the actionability wait when count()
# already confirmed the element is present — downstream interactions
# use page.evaluate() which bypasses the same check.
if candidate_count and candidate_count > 0:
return locator.last

# Fallback: when count() raised an exception above (candidate_count
# is None), attempt the original wait_for path. This is unlikely to
# succeed given the same patchright quirk, but preserves the prior
# behaviour for non-patchright drivers where wait_for works normally.
candidate = locator.last
try:
await candidate.wait_for(state="visible")
Expand Down Expand Up @@ -2354,8 +2369,18 @@ async def send_message(
await handle_modal_close(self._page)
display_name = await self._read_profile_display_name()
if profile_urn:
# Build the full compose URL that LinkedIn's own Message button
# generates. The minimal ?recipient=<URN> form works for established
# connections but shows a "Say hello" widget (no compose box) for new
# connections. Adding profileUrn + screenContext + interop=msgOverlay
# consistently opens the real composer regardless of connection age.
_encoded = quote_plus(f"urn:li:fsd_profile:{profile_urn}")
compose_url: str | None = (
f"https://www.linkedin.com/messaging/compose/?recipient={profile_urn}"
f"https://www.linkedin.com/messaging/compose/"
f"?profileUrn={_encoded}"
f"&recipient={profile_urn}"
f"&screenContext=NON_SELF_PROFILE_VIEW"
f"&interop=msgOverlay"
)
else:
compose_url = await self._resolve_message_compose_href()
Expand Down Expand Up @@ -2448,47 +2473,60 @@ async def send_message(
recipient_selected=recipient_selected,
)

await compose_box.click()
await compose_box.press_sequentially(message, delay=30)
await asyncio.sleep(0.3)

try:
await self._page.wait_for_function(
"""() => {
const isVisible = element =>
!!(
element &&
(element.offsetWidth ||
element.offsetHeight ||
element.getClientRects().length)
);
return Array.from(
document.querySelectorAll(
'button[type="submit"], button[aria-label*="Send"], button[aria-label*="send"]'
)
).some(button => isVisible(button) && !button.disabled);
}""",
)
except PlaywrightTimeoutError:
# patchright quirk: compose_box.click() and press_sequentially() use
# actionability checks internally and hit the same wait_for timeout.
# Instead: focus via page.evaluate() (no actionability check) and type
# via page.keyboard.type() which operates on the active element directly
# and fires the real keydown/input/keyup events React needs to enable Send.
#
# DOM dependency: innerText extraction is not applicable here — we need
# to call .focus() on the element reference, which requires querySelector.
# Selectors use only role + contenteditable + aria-label (ARIA attributes,
# not layout class names) so they are stable across LinkedIn UI changes.
focused = await self._page.evaluate(
"""() => {
const el = document.querySelector(
'div[role="textbox"][contenteditable="true"][aria-label*="Write a message"],'
+ 'div[role="textbox"][contenteditable="true"]'
);
if (!el) return false;
el.focus();
return true;
}"""
)
if not focused:
await self._dismiss_message_ui()
return self._message_action_result(
self._page.url,
"send_unavailable",
"LinkedIn did not expose an enabled Send action for this draft.",
"compose_interact_failed",
"Could not focus compose box via JavaScript.",
recipient_selected=recipient_selected,
)
await asyncio.sleep(0.1)
await self._page.keyboard.type(message, delay=15)
await asyncio.sleep(0.3)

send_button = self._page.locator(_MESSAGING_ENABLED_SEND_SELECTOR).last
try:
await send_button.click()
except PlaywrightTimeoutError:
await self._dismiss_message_ui()
return self._message_action_result(
self._page.url,
"send_unavailable",
"LinkedIn did not confirm that the message was sent.",
recipient_selected=recipient_selected,
)
# patchright actionability also blocks send_button.click(). Use JS click
# on any visible, enabled send button; fall back to Enter key which
# LinkedIn's composer also accepts for submission.
#
# DOM dependency: we need btn.click() on the element reference — not
# achievable via innerText or URL navigation. Selectors use only type,
# aria-label, and data attributes (no layout class names).
await asyncio.sleep(1.0) # allow React to process keyboard input
sent_via_js = await self._page.evaluate(
"""() => {
const btn = Array.from(document.querySelectorAll(
'button[type="submit"], button[aria-label*="Send"], button[aria-label*="send"],'
+ 'button[data-control-name="send"]'
)).find(b => !b.disabled && (b.offsetWidth || b.offsetHeight || b.getClientRects().length));
if (!btn) return false;
btn.click();
return true;
}"""
)
if not sent_via_js:
await self._page.keyboard.press("Enter")

if not await self._message_text_visible(message):
await self._dismiss_message_ui()
Expand Down
Loading
Loading