Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add check for selector visibility, revert domcontentloaded to networkidle state #257

Merged
merged 7 commits into from
Dec 14, 2024

Conversation

RohitR311
Copy link
Collaborator

@RohitR311 RohitR311 commented Dec 14, 2024

  1. Added selector visibility check to resolve empty data scraped issue
  2. Reverted back the waitForLoadState param from domcontentloaded to networkidle

Summary by CodeRabbit

  • New Features

    • Enhanced logic for handling selectors, ensuring distinct selectors are returned.
    • Improved error handling for ad-blocker methods.
  • Bug Fixes

    • Optimized selector retrieval process and robustness in checking actionable selectors.
  • Chores

    • Updated timeout settings for navigation and selector checks to accommodate longer loading times.

Copy link

coderabbitai bot commented Dec 14, 2024

Walkthrough

The pull request introduces enhancements to the Interpreter class in the maxun-core/src/interpret.ts file. The changes focus on improving selector handling, error management, and workflow execution. Key modifications include optimizing the getSelectors method to use a Set for unique selector collection, updating the getState method with a more robust selector checking approach, and setting a default navigation timeout of 100,000 milliseconds in the run method.

Changes

File Change Summary
maxun-core/src/interpret.ts - Updated getSelectors to use Set for unique selectors
- Modified getState method to use waitForSelector with 'attached' state
- Added default navigation timeout of 100,000ms in run method
- Improved error handling in applyAdBlocker and disableAdBlocker methods

Sequence Diagram

sequenceDiagram
    participant Interpreter
    participant Page
    participant Workflow
    Interpreter->>Page: setDefaultNavigationTimeout(100000)
    Interpreter->>Workflow: getSelectors()
    Workflow-->>Interpreter: Unique Selectors
    Interpreter->>Page: waitForSelector(selector, {state: 'attached', timeout: 10000})
    Page-->>Interpreter: Selector State
Loading

Possibly related PRs

Suggested labels

Type: Feature, Type: Enhancement, Status: In Review

Suggested reviewers

  • amhsirak

Poem

🐰 Selectors dance, a digital ballet,
Timeouts stretch wide, no page shall delay!
Set in motion, with precision so neat,
Interpreter's magic makes workflows complete 🌟
A rabbit's code, swift and bright! 🚀

Tip

CodeRabbit's docstrings feature is now available as part of our Early Access Program! Simply use the command @coderabbitai generate docstrings to have CodeRabbit automatically generate docstrings for your pull request. This feature will be included in our Pro Plan when released.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@RohitR311 RohitR311 marked this pull request as draft December 14, 2024 15:06
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (3)
maxun-core/src/interpret.ts (3)

195-196: Remove commented-out code to improve readability

Lines 195-196 contain commented-out code. If this code is no longer needed, consider removing it to keep the codebase clean and improve readability.


Line range hint 172-179: Review the early return in getSelectors; it might not collect all selectors

In the getSelectors method, the loop iterates from the end of the workflow to the beginning. Upon finding the first non-empty selectors, it adds them to selectorsSet and immediately returns. This means that only selectors from the last workflow step with selectors are collected, and earlier selectors are not considered. If the intention is to collect all unique selectors from the entire workflow, you might want to remove the return statement inside the loop so that the loop continues through all steps.

Apply this diff to collect all selectors:

for (let index = workflow.length - 1; index >= 0; index--) {
    const currentSelectors = workflow[index]?.where?.selectors;

    if (currentSelectors && currentSelectors.length > 0) {
        currentSelectors.forEach((selector) => selectorsSet.add(selector));
-       return Array.from(selectorsSet);
    }
}

return Array.from(selectorsSet);

781-782: Consider making the navigation timeout configurable

Setting the default navigation timeout to 100,000 milliseconds (100 seconds) in the run method may not be appropriate in all cases. Long timeouts can mask issues with slow-loading pages and may lead to inefficient resource usage. Consider making this timeout a configurable parameter or handling timeouts on a per-case basis to provide flexibility.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4469325 and 7ce7a15.

📒 Files selected for processing (1)
  • maxun-core/src/interpret.ts (4 hunks)

Comment on lines +217 to +227

const presentSelectors: SelectorArray = await Promise.all(
selectors.map(async (selector) => {
try {
await page.waitForSelector(selector, { state: 'attached' });
return [selector];
} catch (e) {
return [];
}
}),
).then((x) => x.flat());
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Potential issue: waitForSelector with state: 'attached' doesn't ensure visibility or interactability

The updated code uses waitForSelector with state: 'attached', which only ensures that the elements are present in the DOM, not that they are visible or interactable. If the goal is to interact with or scrape these elements, you should use state: 'visible' to ensure the elements are visible. Alternatively, consider checking for both visibility and enabled state to ensure the elements are actionable.

Apply this diff to ensure selectors are visible:

const presentSelectors: SelectorArray = await Promise.all(
    selectors.map(async (selector) => {
        try {
-           await page.waitForSelector(selector, { state: 'attached' });
+           await page.waitForSelector(selector, { state: 'visible' });
            return [selector];
        } catch (e) {
            return [];
        }
    }),
).then((x) => x.flat());
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const presentSelectors: SelectorArray = await Promise.all(
selectors.map(async (selector) => {
try {
await page.waitForSelector(selector, { state: 'attached' });
return [selector];
} catch (e) {
return [];
}
}),
).then((x) => x.flat());
const presentSelectors: SelectorArray = await Promise.all(
selectors.map(async (selector) => {
try {
await page.waitForSelector(selector, { state: 'visible' });
return [selector];
} catch (e) {
return [];
}
}),
).then((x) => x.flat());

@RohitR311 RohitR311 changed the base branch from develop to webdrive-patch December 14, 2024 15:30
@amhsirak amhsirak marked this pull request as ready for review December 14, 2024 15:55
@amhsirak amhsirak merged commit ea2c36f into webdrive-patch Dec 14, 2024
1 check passed
@amhsirak amhsirak added the Type: Enhancement Improvements to existing features label Dec 14, 2024
@amhsirak amhsirak changed the title feat: add check for selector visibility, revert domcontentloaded to networkidle state feat: add check for selector visibility, revert domcontentloaded to networkidle state Dec 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Enhancement Improvements to existing features
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants