Coder Social home page Coder Social logo

Comments (11)

stevepryde avatar stevepryde commented on July 21, 2024 1

Hi, the Capabilities struct is really limited right now but you can easily extend it with raw JSON so it should be possible to add any capabilities as long as you know the format.

For Chrome options, I believe you can do things like this:

let mut caps = DesiredCapabilities::chrome();
caps.add("goog:chromeOptions", serde_json::json!({"args": ["--disable-web-security"]}));

I only tried with --headless and it seemed to work. Let me know if it works for you.

from thirtyfour.

stevepryde avatar stevepryde commented on July 21, 2024 1

FYI - as of v0.6.0 (just deployed) you can now do:

let mut caps = DesiredCapabilities::chrome();
caps.add_arg("--disable-web-security")?;

The interface should actually be backwards compatible so people can still construct the capabilities struct manually if preferred.

from thirtyfour.

stevepryde avatar stevepryde commented on July 21, 2024 1

As I understand it, generally you should not use blocking calls like thread::sleep() during async functions, since this will block that thread and potentially prevent other futures from running until you next .await.
If you're using tokio, have a look at https://docs.rs/tokio/0.2.11/tokio/time/fn.delay_for.html.

However, back in the world of website automation, it's also generally not ideal to use sleeps of any form to wait for things to happen on the website. Always prefer polling instead of sleeps where possible. This is because any code that uses sleeps is quite prone to timing issues and will either sometimes fail if the website was too fast/slow or otherwise your code might sleep unnecessarily long in places.

If the first click causes some element to appear or disappear, then you can effectively wait for that event to happen by repeatedly polling for that condition, and only fail after a certain number of tries (or after a certain amount of time). The thirtyfour crate is intended to be lower level than this and will stick fairly close to the selenium spec, but you can write high-level abstractions that take care of repeatedly calling find_element or checking whether an element has a particular attribute, until either the condition is met or a timeout is reached.

If you go down this path, sometimes it is better to set selenium's implicit wait timeout to 0 (via the WebDriver::implicitly_wait() method) so that you don't end up with two different timeouts interfering with each other. You might also still want to call tokio's delay method to delay 500ms or so between polling attempts. It really depends on what you're trying to do and the website you're interacting with.

However, the simplest way, at least when starting out, is to just search for the element you expect to appear, relying on selenium's implicit wait to sleep for you. Thirtyfour automatically sets the implicit wait to 30 seconds, but you can change it to whatever you need. You can also use CSS or XPath selectors to either wait for an element with specific attributes, or even wait for an existing element to gain specific attributes.

Hope that helps

from thirtyfour.

stevepryde avatar stevepryde commented on July 21, 2024 1

I tried this in python and it doesn't work there either.
Try this instead:

driver.execute_script("window.open('about:blank', '_blank');", &ScriptArgs::new()).await?;
let tabs = driver.window_handles().await?;
driver.switch_to().window(&tabs[1]).await?;

You can also replace the about:blank with the url you want the new tab to navigate to.

from thirtyfour.

longjin1991 avatar longjin1991 commented on July 21, 2024

It work, thank for your help.

from thirtyfour.

longjin1991 avatar longjin1991 commented on July 21, 2024

Thank you,

  • How to set delay time when click or input element.
    ex:
    elem1.click().await?;
    thread::sleep(time::Duration::from_secs(2)); //set delay time 2 second
    elem2.click().await?;

  • How to open New tab?

  • Add set time out to reload page?

from thirtyfour.

longjin1991 avatar longjin1991 commented on July 21, 2024

thank for your help, Can you tell me how to open a new tab with thirtyfour same here:
https://www.testingexcellence.com/open-new-tab-browser-using-selenium-webdriver-java/

from thirtyfour.

stevepryde avatar stevepryde commented on July 21, 2024

The code to open a new tab would be almost the same as the example you linked to.
Something like this (I have not tested it):

driver.find_element(By::Css("body")).await?.send_keys(Keys::Control + "t").await?;
let tabs = driver.window_handles().await?;
driver.switch_to().window(tabs[0]).await?;

You would need to check whether the new tab is the first element in the array.
Btw, feel free to browse the docs at https://docs.rs/thirtyfour. If anything is not clear in the docs I would be happy to update them.

from thirtyfour.

longjin1991 avatar longjin1991 commented on July 21, 2024

I had test with your code but don't work, it don't open new tab:
driver.find_element(By::Css("body")).await?.send_keys(Keys::Control + "t").await?;

from thirtyfour.

stevepryde avatar stevepryde commented on July 21, 2024

That will sent Ctrl+t to the body element. It's possible that doesn't work on the page you are on. It might be worth trying the same in python or Java and seeing if that works.

from thirtyfour.

longjin1991 avatar longjin1991 commented on July 21, 2024

the code is worked, thank you.

from thirtyfour.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.